Home > database >  Elixir CSV Issue
Elixir CSV Issue

Time:09-23

UPDATE:

CSV now looks like this:

01|Data1\n02|Data2\n01|Data3

and no errors have thrown.

However the IO inspect prints an email array / list?

[]

so I not sure if that is something to do with my code?

ORIGINAL POST:

I am getting the following error:

(NimbleCSV.ParseError) unexpected escape character " in "01|Data1\",\"02|Data2\",\"01|Data3"

The CSV file is as follows:

01|Data1","02|Data2","01|Data3

But I have also tried: (thinking I was missing the start / end char in the data)

"01|Data1","02|Data2","01|Data3"

This is the Elixir code I have:

NimbleCSV.define(MyParser, separator: "|", escape: "\"")

File.stream!("data.csv")
|> NimbleCSV.RFC4180.parse_stream()
|> Stream.map(fn [code, data] ->
  %{code: code, data: data}
end)
|> Enum.to_list()
|> IO.inspect()

The NimbleCSV is at the top of my file, outside the start of the module.

CodePudding user response:

NimbleCSV expects the first line to be a header:

NimbleCSV.define(MyParser, separator: "|")

"""
code|data
01|Data1
02|Data2
01|Data3
"""
|> MyParser.parse_string()

Output:

[["01", "Data1"], ["02", "Data2"], ["01", "Data3"]]
  • Related