ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - CargoSense/ex_csv: CSV for Elixir
CSV for Elixir. Contribute to CargoSense/ex_csv development by creating an account on GitHub.
Visit Site

GitHub - CargoSense/ex_csv: CSV for Elixir

GitHub - CargoSense/ex_csv: CSV for Elixir

ExCsv

Elixir CSV.

Note: Currently only supports parsing.

Usage

Parsing

Parsing a file gives you a ExCsv.Table struct:

File.read!("foo/bar.csv") |> ExCsv.parse
# => {:ok, %ExCsv.Table{...}}

(You can alse use ExCsv.parse!/1 which will raise an error instead of returning an {:error, err} tuple if parsing fails.)

If your CSV has headings, you can let the parser know up front:

{:ok, table} = File.read!("foo/bar.csv") |> ExCsv.parse(headings: true)
# => {:ok, %ExCsv.Table{...}}
table.headings
# => ["Person", "Current Age"]

Or you can use ExCsv.with_headings/1 afterwards:

{:ok, table} = File.read!("foo/bar.csv")
               |> ExCsv.parse!
               |> ExCsv.with_headings
# => %ExCsv.Table{...}
table.headings
# => ["Person", "Current"]

You can also change the set or change headings by using ExCsv.with_headings/2:

table = File.read!("foo/bar.csv")
        |> ExCsv.parse!
        |> ExCsv.with_headings(["name", "age"])
# => %ExCsv.Table{...}
table.headings
# => ["name", "age"]

If you need to parse a format that uses another delimiter character, you can set it as an option (note the single quotes):

table = File.read!("foo/bar.csv") |> ExCsv.parse!(delimiter: ';')
# => %ExCsv.Table{...}

Once you have a ExCsv.Table, you can use its headings and body directly -- or you enumerate over the table.

Enumerating

If your ExCsv.Table struct does not have headers, iterating over it will result in a list for each row:

table = File.read!("foo/bar.csv")
        |> ExCsv.parse!
        |> Enum.to_list
# [["Jayson", 23], ["Jill", 34], ["Benson", 45]]

If your table has headings, you'll get maps:

table = File.read!("foo/bar.csv")
        |> ExCsv.parse!(headings: true)
        |> ExCsv.with_headings([:name, :age])
        |> Enum.to_list
# [%{name: "Jayson", age: 23},
#  %{name: "Jill", age: 34},
#  %{name: "Benson", age: 45}]

You can build structs from the rows by using ExCsv.as/1 (if the headings match the struct attributes):

table = File.read!("foo/bar.csv")
        |> ExCsv.parse!(headings: true)
        |> ExCsv.with_headings([:name, :age])
        |> ExCsv.as(Person)
        |> Enum.to_list
# [%Person{name: "Jayson", age: 23},
#  %Person{name: "Jill", age: 34},
#  %Person{name: "Benson", age: 45}]

If the headings don't match the struct attributes, you can provide a mapping (of CSV heading name to struct attribute name) with ExCsv.as/2:

table = File.read!("books.csv")
        |> ExCsv.parse!(headings: true)
        |> ExCsv.as(Author, %{"name" => :title, "author" => :name})
        |> Enum.to_list
# [%Author{name: "John Scalzi", title: "A War for Old Men"},
#  %Author{name: "Margaret Atwood", title: "A Handmaid's Tale"}]

Contributing

Please fork and send pull requests (preferably from non-master branches), including tests (ExUnit.Case).

Report bugs and request features via Issues; PRs are even better!

License

The MIT License (MIT)

Copyright (c) 2014 CargoSense, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory