Install NimbleCSV via mix
masterAdd nimble_csv to your mix.exs dependencies to use the library in your Elixir project.
def deps do
[
{:nimble_csv, "~> 1.1"}
]
endrepository·master·Indexed 21 days ago
https://github.com/dashbitco/nimble_csvA high-performance CSV parsing and dumping library for Elixir. It allows developers to define custom parsers using efficient binary patterns via NimbleCSV.define/2 or use the built-in RFC4180 parser. Supports lazy streaming of large files via parse_stream/1.
Add nimble_csv to your mix.exs dependencies to use the library in your Elixir project.
def deps do
[
{:nimble_csv, "~> 1.1"}
]
endFor the most common CSV implementation (comma as separator and double-quote as escape), use NimbleCSV.RFC4180. You can alias it to CSV for convenience.
alias NimbleCSV.RFC4180, as: CSV
CSV.parse_string("name,age\njohn,27")
#=> [["john","27"]]NimbleCSV supports lazy (streaming) parsing via parse_stream/1. This is useful for processing large files without loading them entirely into memory. You can pipe a file stream directly into the parser and then use standard Elixir Stream functions to transform the data.
# Lazily parses a file stream
"path/to/file"
|> File.stream!
|> MyParser.parse_stream()
|> Stream.map(fn [name, age] ->
%{name: :binary.copy(name), age: String.to_integer(age)}
end)
|> Stream.run()NimbleCSV allows you to define custom parsers/dumpers using NimbleCSV.define/2. This is equivalent to calling defmodule and should be done at the top of your file. You can specify a separator and an escape character. The resulting module provides parsing capabilities optimized via binary patterns.
# Define the parser (this is equivalent to calling
# defmodule and should be done at the top of a file)
NimbleCSV.define(MyParser, separator: "\t", escape: "\"")
# Parse the data
MyParser.parse_string("name\tage\njohn\t27")
#=> [["john","27"]]