CsvSniffer v0.1.0 CsvSniffer View Source

An Elixir port of Python's CSV Sniffer.

Link to this section Summary

Functions

"Sniffs" the format of a CSV file (i.e. delimiter, quote character).

Link to this section Functions

Link to this function

sniff(data, opts \\ [])

View Source (since 0.1.0)
sniff(data :: Enumerable.t() | binary(), [{:delimiters, [String.t()]}]) ::
  {:ok, CsvSniffer.Dialect.t()} | {:error, reason :: any()}

"Sniffs" the format of a CSV file (i.e. delimiter, quote character).

Options

  • :delimiters - Limit the sniffer to a list of possible delimiters.

Examples

Accepts enumerables (such as file streams):

iex> """
...> Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
...> Shark City, Glendale Heights, IL, 12/28/02, Prezence
...> Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow
...> Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
...> """
...> |> String.split("\n")
...> |> CsvSniffer.sniff()
{:ok, %CsvSniffer.Dialect{
  delimiter: ",",
  quote_character: "\"",
  double_quote: false,
  skip_initial_space: true
}}

Also accepts strings, which will be split on newlines:

iex> """
...> Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
...> Shark City, Glendale Heights, IL, 12/28/02, Prezence
...> Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow
...> Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
...> """
...> |> CsvSniffer.sniff()
{:ok, %CsvSniffer.Dialect{
  delimiter: ",",
  quote_character: "\"",
  double_quote: false,
  skip_initial_space: true
}}