NimbleCSV Documentation

repository·master·Indexed 21 days ago

https://github.com/dashbitco/nimble_csv

A 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.

Tokens
532
Snippets
4
Records
4
Agent score
24%

What's inside NimbleCSV

  1. Use the built-in RFC4180 parser

    master

    For 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"]]
  2. Lazily parse a file stream

    master

    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()
  3. Define a custom CSV parser

    master

    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"]]