`headers: true` option doesn't work anymore in version 3.*
ribanez7 opened this issue · 5 comments
Steps to reproduce it
csv = ["header_one,header_two", "row_one,row_two"]
CSV.decode!(csv, headers: true) |> Enum.to_list()
The previous example results on empty list in the 3.* versions.
In previous versions it results in:
[%{"header_one" => "row_one", "header_two" => "row_two"}]
Interesting, thanks for sharing! When adding newlines, this should still work:
csv = ["header_one,header_two\n", "row_one,row_two\n"]
CSV.decode!(csv, headers: true) |> Enum.to_list()
This is indeed changed behaviour since 2.x would allow to specify a line by line feed without newlines. When reading from files, we usually get the newlines / delimiters at the end of the line in the stream, and they are part of CSV grammar.
Is this a use case you are encountering? If yes, what’s the source of the CSV? This will help me document the change.
If you get a stream of lines missing delimiters from a source you don’t control, you can map the stream to add newlines before passing it to CSV
:
source |>
Stream.map(fn s -> s <> "\n" end) |>
CSV.decode!(headers: true)
This carries a small performance penalty. You probably only want to apply it if for some reason adding newlines at the source is not an option.
Just to add to the thought above, if you have a string with csv data, you would have had to do this in 2.x:
"string,of,csv,data\nsome,data,with,headers" |> String.split("\n") |> CSV.decode(headers: true)
whereas in 3.x you can simply do:
["string,of,csv,data\nsome,data,with,headers"] |> CSV.decode(headers: true)
Not sure if that applies to your case.
Hello @beatrichartz , thanks for your answer, the cases I faced were in tests, but I still haven't tried with real CSVs, since in the test I was faking the stream, and that's why I don't have the newline character. Let me try with real files and I will let you know. If I see no problem, then I will simply add this newline into my fake data for the tests. 👍🏼
All looks good! thanks for adding this into the migration notes.