beatrichartz / csv

CSV Decoding and Encoding for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to specify header values and ignore headers in the csv file

wsmoak opened this issue · comments

Thanks for the CSV library!

In order to use the Map parsed from each row with Ecto, I need to define the headers as atoms that exactly match my model:

File.stream!("/Users/wsmoak/Downloads/transactions.csv")
  |> CSV.decode(headers: [:date, :description, :original_description, :amount, :transaction_type, :category, :account_name, :labels, :notes])

When I do this, the first line of the file that contains the headers is picked up as a row of values, which I don't want.

Is there a way to skip that first row the way headers: false would, and define the values for the headers for the map?

Would having two different attributes work better? Maybe :headers could be true/false only, to say whether the csv file contains headers, and then a separate attribute could be used to optionally specify the header values to use?

(My workaround is to define a function that matches on one of the things I know will be in the Map for that first unwanted record, and ignore it: def store_it(%{:date => "Date"}) do ... end )

commented

Interesting usecase, thanks for raising this!

I think in this case the solution could be done outside the CSV module by piping in Stream.drop(1) before feeding the stream into decode:

File.stream!("/Users/wsmoak/Downloads/transactions.csv")
  |> Stream.drop(1)
  |> CSV.decode(headers: [:date, :description, :original_description, :amount, :transaction_type, :category, :account_name, :labels, :notes])

The library does not filter or skip rows in any case - headers: false lets the library read the first row as a data row, headers: true reads it as a headers row. I quite like having any skipping or filtering done before feeding the stream into the library since it makes the intention clear and keeps these decisions out of the library.

Hope this works for you, feel free to reopen if you have more questions / this is turning into a bug!

Thanks, that works!