kgiszczak / shale

Shale is a Ruby object mapper and serializer for JSON, YAML, TOML, CSV and XML. It allows you to parse JSON, YAML, TOML, CSV and XML data and convert it into Ruby data structures, as well as serialize data structures into JSON, YAML, TOML, CSV or XML.
https://shalerb.org/
MIT License
618 stars 19 forks source link

Selecting columns while importing CSV #26

Closed dpaluy closed 8 months ago

dpaluy commented 9 months ago

I have a big CSV file that I'd like to extract several columns.

Previously, I would resolve those:

  valid_columns = %w[header1 header5 header24]
  filtered_rows = []
  CSV.parse(csv_content, headers: true) do |row|
    filtered_row = row.to_h.slice(*valid_columns)
    filtered_rows << filtered_row
  end
  filtered_rows

I tried Person.from_csv(context, headers: true, only: valid_columns) but it ignores the option only.

If I explicitly map each column as an attribute to be ignored, that works. Is there a more efficient way to do this?

kgiszczak commented 9 months ago

At the moment you have to explicitly map every column, and then you can use only option to pick the ones you're interested in.