Read a CSV file and convert it into list of tuples

Hey, I wanted to read a CSV file and convert it into a list of tuples and I come up with this:

    "./pairs.csv"
    |> Path.expand(__DIR__)
    |> File.stream!
    |> Stream.drop(1)
    |> Stream.map(&String.trim(&1, "\n"))
    |> Stream.map(&String.split(&1, ","))
    |> Enum.map(fn(pairs) -> {elem(Float.parse(List.first(pairs)), 0), elem(Float.parse(List.last(pairs)), 0)} end)

Is there any simpler and robust way to do this? How about the performance how can I improve it?

@azbshiri What you have seems largely fine although the formatting makes it a bit hard to read.

If you want optimal performance though use https://github.com/plataformatec/nimble_csv/, it does a variety of things to result in optimal match performance.

3 Likes

Oh, thanks for the update.