aura999
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
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You’re trying this but your code is not streaming into a csv. It’s streaming data out of an csv into data.
parse_streamis parsing a stream of csv data into elixir data. The reverse would bedump_to_stream, which turns elixir data into a stream for csv formatted data.aura999
Okay thanks. So if I already have a stream do I just write it straight into the file without using
dump_to_stream/1?Also as per this NimbleCSV — NimbleCSV v1.3.0 since the description says
Lazily parses CSV from a stream and returns a stream of rows.I was assumingparse_stream/2could be passed a stream and it would output a streamLostKobrakai
Streams are lazy enumerables. Being a stream doesn’t tell you anything about what data the streams deals with.
parse_streammaps from a stream of csv formatted binaries to a stream of rows being lists of cell contents. It’s the lazy version toparse_enumerable, which immediately returns[[binary()]].In your case I’m really confused though because if you already have a csv file and you want to write that csv file, then there’s no need for NimbleCSV in the first place.
Enum.into(content_stream, file_collectable)would be all you need.What makes
streamseven more confusing here is that aFile.Streamstruct implements bothEnumerable.t(a - in this case lazy – collection to enumerate) as well asCollectable.t(a collection, which items can be pushed into), which work independently. You’re using the latter functionality here.aura999
Thanks for the detailed info @LostKobrakai. Appreciate it