Typedcsv
Here's your standard untyped CSV:
name,income,created_at,,great
Seamus,12301.2,2012-02-21,red;blue,true
Now, you and I know that 12301.2 is a number and 2012-02-21 is a date and red;blue is a list... so let's just write that into the headers:
name,income:number,created_at:date,tags:list,great:boolean
Seamus,12301.2,2012-02-21,red;blue,true
Now let's parse it:
Typedcsv.foreach('file.csv', headers: true) do |row|
row['income'] # will be a Float
row['created_at'] # will be a Date
row['tags'] # will be an Array
row['great'] # will be TrueClass or FalseClass
end
This gem provides Typedcsv.foreach(), which takes exactly the same arguments as ruby stdlib CSV.foreach.
Types
- text (default)
- number
- list (must be semicolon-separated)
- date (must be ISO8601)
- time (must be ISO8601)
- boolean (recognizes "true" or "false")
Benchmarks
It's about 10x slower than ruby's stdlib CSV.foreach:
cd benchmark && ruby benchmark.rb
[...]
CSV.foreach - array mode
2.503 (
Sponsor
We use typedcsv for B2C customer intelligence at Faraday.
Copyright
Copyright 2017 Faraday