marcin

marcin OP

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.

First 5 of 5 Posts Switch mode

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.

dimitarvp

dimitarvp

iex> Timex.parse!("2016-12-01 10:36:29.772554", "%Y-%m-%d %H:%M:%S.%f", :strftime)
~N[2016-12-01 10:36:29.772554]

Not sure if you are receiving datetimes formatted in other ways but this should sort you up as a start.

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.

dimitarvp

dimitarvp

Nope, you aren’t missing anything. I simply prefer being explicit – plus I can never remember what passes for which standard.

peerreynders

peerreynders

I guess if you already need Timex for something else.

I’d be willing to massage the string a bit to be able to use the standard functionality and ditch a dependency - and ISO8601 has pretty good coverage (am/pm could be a bit awkward).

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement