Skip to content

Instantly share code, notes, and snippets.

@Zensavona
Created February 26, 2017 21:47
Show Gist options
  • Save Zensavona/6f6d270f3beda1c237460b130eff7ce7 to your computer and use it in GitHub Desktop.
Save Zensavona/6f6d270f3beda1c237460b130eff7ce7 to your computer and use it in GitHub Desktop.
# in real life you probably won't store the file on disk, you'll just let users upload it in phoenix
# and have it's value available as a variable
alias NimbleCSV.RFC4180, as: CSV
# if you're reading from disk
"path/to/csv/file"
|> File.stream!(read_ahead: 100_000)
|> CSV.parse_stream
|> Stream.map(fn [name, age] ->
%{name: name, age: String.to_integer(age)}
end)
# if you've got the file's contents in memory or you stored it in the database and retreived it
"name,age,john,27"
|> CSV.parse_string
|> Enum.map(fn [name, age] ->
%{name: name, age: String.to_integer(age)}
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment