DataMorph v0.0.7 DataMorph.Csv View Source

Functions for converting enumerable, or string of CSV to stream of rows.

Link to this section Summary

Functions

Parse csv string, stream, or enumerable to stream of rows

Link to this section Functions

Link to this function to_headers_and_rows_stream(csv) View Source

Parse csv string, stream, or enumerable to stream of rows.

Examples

Convert blank string to empty headers and empty stream.

iex> {headers, rows} = DataMorph.Csv.to_headers_and_rows_stream("")
...> rows
...> |> Enum.to_list
[]
...> headers
[]

Map a string of lines separated by \n to headers, and a stream of rows as lists:

iex> {headers, rows} = "name,iso\n" <>
...> "New Zealand,nz\n" <>
...> "United Kingdom,gb"
...> |> DataMorph.Csv.to_headers_and_rows_stream
...> rows
...> |> Enum.to_list
[
  ["New Zealand","nz"],
  ["United Kingdom","gb"]
]
...> headers
["name","iso"]

Map a stream of lines separated by \n to headers, and a stream of rows as lists:

iex> {headers, rows} = "name,iso\n" <>
...> "New Zealand,nz\n" <>
...> "United Kingdom,gb"
...> |> String.split("\n")
...> |> Stream.map(& &1)
...> |> DataMorph.Csv.to_headers_and_rows_stream
...> rows
...> |> Enum.to_list
[
  ["New Zealand","nz"],
  ["United Kingdom","gb"]
]
...> headers
["name","iso"]

Map a string of tab-separated lines separated by \n to headers, and a stream of rows as lists:

iex> {headers, rows} = "name\tiso\n" <>
  ...> "New Zealand\tnz\n" <>
  ...> "United Kingdom\tgb"
  ...> |> DataMorph.Csv.to_headers_and_rows_stream(separator: ?\t)
  ...> rows
  ...> |> Enum.to_list
  [
    ["New Zealand","nz"],
    ["United Kingdom","gb"]
  ]
  ...> headers
  ["name","iso"]
Link to this function to_headers_and_rows_stream(csv, options) View Source