DEV Community

Marcøs
Marcøs

Posted on • Edited on

1

Elixir - csv import

See Files on Github
country-codes.csv
create_country_table.exs

This post expands on this article from 'Experimenting with Code' to only import a subset of csv columns into Postgres using Ecto.

In this case, we are only concerned with the code and name columns from country-codes.csv. This example uses File.stream() and CSV.decode!() to get just the two columns and pass it to the COPY command.

file_path
|> File.stream!()
|> CSV.decode!(headers: true)
|> Stream.map(fn row ->
  [
    row["code"],
    row["name"]
  ]
end)
|> CSV.encode()
|> Enum.into(stream)
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay