Popularity
8.9
Growing
Activity
0.0
Stable
444
4
87

Monthly Downloads: 1,395,496
Programming language: Elixir
License: MIT License
Tags: CSV    
Latest version: v2.5.0

csv alternatives and similar packages

Based on the "CSV" category.
Alternatively, view csv alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of csv or a related project?

Add another 'CSV' Package

README

CSV Build Status Coverage Status Inline docs Hex pm Hex Docs License Downloads

RFC 4180 compliant CSV parsing and encoding for Elixir. Allows to specify other separators, so it could also be named: TSV. Why it is not idk, because of defaults I think.

Why do we want it?

It parses files which contain rows (in UTF-8) separated by either commas or other separators.

If that's not enough reason to absolutely :heart: it, it also parses a CSV file in order about 2x times as fast as an unparallelized stream implementation :rocket:

When do we want it?

Now.

How do I get it?

Add

{:csv, "~> 2.5"}

to your deps in mix.exs like so:

defp deps do
  [
    {:csv, "~> 2.5"}
  ]
end

Elixir version requirements

  • Elixir 1.5.0 is required for all versions above 2.5.0.
  • Elixir 1.1.0 is required for all versions above 1.1.5.

Great! How do I use it right now?

There are two interesting things you want to do regarding CSV - encoding end decoding.

Decoding

Do this to decode:

File.stream!("data.csv") |> CSV.decode

And you'll get a stream of row tuples:

[ok: ["a", "b"], ok: ["c", "d"]]

And, potentially error tuples:

[error: "Row has length 3 - expected length 2 on line 1", ok: ["c", "d"]]

Use the bang to decode! into a two-dimensional list, raising errors as they occur:

File.stream!("data.csv") |> CSV.decode!

Be sure to read more about decode and its angry sibling decode!

Encoding

Do this to encode a table (two-dimensional list):

table_data |> CSV.encode

And you'll get a stream of lines ready to be written to an IO. So, this is writing to a file:

file = File.open!("test.csv", [:write, :utf8])
table_data |> CSV.encode |> Enum.each(&IO.write(file, &1))

I have this file, but it's tab-separated :interrobang:

Pass in another separator to the decoder:

File.stream!("data.csv") |> CSV.decode(separator: ?\t)

If you want to take revenge on whoever did this to you, encode with semicolons like this:

your_data |> CSV.encode(separator: ?;)

You can also specify headers when encoding, which will encode map values into the right place:

[%{"a" => "value!"}] |> CSV.encode(headers: ["z", "a"])
# ["z,a\\r\\n", ",value!\\r\\n"]

You can also specify a keyword list, the keys of the list will be used as the keys for the rows, but the values will be the value used for the header row name in CSV output

[%{a: "value!"}] |> CSV.encode(headers: [a: "x", b: "y"])
# ["x,y\\r\\n", "value!,\\r\\n"]

You'll surely appreciate some more info on encode.

Polymorphic encoding

Make sure your data gets encoded the way you want - implement the CSV.Encode protocol for whatever strange you wish to encode:

defimpl CSV.Encode, for: MyData do
  def encode(%MyData{has: fun}, env \\ []) do
    "so much #{fun}" |> CSV.Encode.encode(env)
  end
end

Or similar.

Ensure performant encoding

The encoding protocol implements a fallback to Any for types where a simple call o to_string will provide unambiguous results. Protocol dispatch for the fallback to Any is very slow when protocols are not consolidated, so make sure you have consolidate_protocols: true in your mix.exs or you consolidate protocols manually for production in order to get good performance.

There is more to know about everything :tm: - Check the doc

Contributions & Bugfixes are most welcome!

Please make sure to add tests. I will not look at PRs that are either failing or lowering coverage. Also, solve one problem at a time.

Copyright and License

Copyright (c) 2022 Beat Richartz

CSV source code is licensed under the MIT License.


*Note that all licence references and agreements mentioned in the csv README section above are relevant to that project's source code only.