View Source CSV.Decoding.Parser (CSV v3.2.1)

The Parser CSV module transforms a stream of byte chunks or bytes into a stream of row tuples and (potentially) error tuples. It follows the grammar defined in RFC4180.

Summary

Functions

Parse a stream of comma-separated lines into a stream of rows. The Parser expects line or variable size byte stream input.

Types

@type parse_options() ::
  {:unescape_formulas, boolean()}
  | {:escape_max_lines, integer()}
  | {:separator, char()}
  | {:escape_character, char()}
  | {:field_transform, (String.t() -> String.t())}

Functions

Link to this function

add_error(rows, error_module, arguments)

View Source
Link to this function

parse(stream, options \\ [])

View Source
@spec parse(Enumerable.t(), [parse_options()]) :: Enumerable.t()

Parse a stream of comma-separated lines into a stream of rows. The Parser expects line or variable size byte stream input.

Options

These are the options:

  • :separator – The separator token to use, defaults to ?,. Must be a codepoint (syntax: ? + (your separator)).
  • :field_transform – A function with arity 1 that will get called with each field and can apply transformations. Defaults to identity function. This function will get called for every field and therefore should return quickly.
  • :unescape_formulas – When set to true, will remove formula escaping inserted to prevent CSV Injection.

Examples

Convert a stream of lines with inlined escape sequences into a stream of rows:

iex> ["a,b\n","c,d\n"]
...> |> Stream.map(&(&1))
...> |> CSV.Decoding.Parser.parse
...> |> Enum.take(2)
[ok: ["a", "b"], ok: ["c", "d"]]

Convert a stream of lines with into a stream of rows trimming each field:

iex> [" a , b   \n"," c   ,   d \n"]
...> |> Stream.map(&(&1))
...> |> CSV.Decoding.Parser.parse(field_transform: &String.trim/1)
...> |> Enum.take(2)
[ok: ["a", "b"], ok: ["c", "d"]]