I am trying to stream into a csv file using nimble_csv(1.2.0) but running into issues.
This is the code I am using to create a csv file from the stream.
defmodule Utils
alias NimbleCSV.RFC4180, as: CSV
def write_to_file(content_stream) do
path = Path.join(System.tmp_dir(), "#{UUID.uuid4(:hex)}.csv")
content_stream
|> CSV.to_line_stream()
|> CSV.parse_stream([skip_headers: false])
|> Stream.into(File.stream!(path, [:write, :utf8]))
|> Stream.run()
end
end
This is my test data
contents = [
"First Name,Last Name,Email\n",
"David,Byrne,david@test.com"
]
|> Stream.map(&String.trim_leading/1)
Utils.write_to_file(contents)
The file is created however the contents in the file are missing the comma separator
and the new line characters
First NameLast NameEmailDavidByrnedavid@test.com
If however I update Utils.write_to_file/1
to by removing parse_stream/2
def write_to_file(content_stream) do
path = Path.join(System.tmp_dir(), "#{UUID.uuid4(:hex)}.csv")
content_stream
|> CSV.to_line_stream()
|> Stream.into(File.stream!(path, [:write, :utf8]))
|> Stream.run()
end
I get the csv contents in the correct format.
First Name,Last Name,Email
David,Byrne,david@test.com
I am not sure if I am missing any options that I need to pass to parse_stream/2
that would cause the contents to be malformed.
Any help would be appreciated