marcin

marcin

How to parse CSV into typed terms?

Hi!

I’d like to parse a CSV rows into typed elements, without knowing what is the schema (column order) of the CSV.

In ruby the CSV library will parse strings into types. In Elixir, both csv and nimble_csv libraries produce string values, and the caller is responsible for casting them into other types (integer, decimal, date-times).

For integer and decimal columns, I can detect if they contain just numbers and decimal point – with regexp or maybe binary matching?
For dates I have no idea how to proceed. I’m aware that date time format is not specified by rfc4180 – files I am parsing have dates in format: “2016-12-01 10:36:29.772554”.

What would you recommend? I am surprised this seemingly simple problem doesn’t have an obvious solution!

m.

Marked As Solved

peerreynders

peerreynders

Am I missing something?

iex(1)> NaiveDateTime.from_iso8601("2016-12-01 10:36:29.772554")
{:ok, ~N[2016-12-01 10:36:29.772554]}

That date can then be “enhanced” with DateTime.from_naive/3.

Also Liked

LostKobrakai

LostKobrakai

Probably because it’s only “seemingly simple”. I can remember cursing at PHP’s strtotime a few times, because parsing dates without knowing the format is hardly more then guesswork and hoping for the best. If you know the format though you should be able to use timex to parse it.

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement