GithubHelp home page GithubHelp logo

Docs about csv HOT 6 CLOSED

simonh1000 avatar simonh1000 commented on September 4, 2024
Docs

from csv.

Comments (6)

beatrichartz avatar beatrichartz commented on September 4, 2024

Interesting - can you elaborate or post some example code you tried and the code that solved it with the other library so I can create an appropriate example?

Thanks!

from csv.

simonh1000 avatar simonh1000 commented on September 4, 2024

Sure

CSVLixir.read("ignore/customers.csv")
|> Enum.to_list
|> Enum.each( fn([id, nm, csr, sal]) ->
        Customer.changeset(%Customer{},
            %{masterid: id,
              custname: nm,
              csrid: String.to_integer(csr),
              salesid: String.to_integer(sal)})
        |> Repo.insert
    end)

from csv.

beatrichartz avatar beatrichartz commented on September 4, 2024

Depending on the version you use and assuming the file contains no headers, these are the solutions I can think of:

v1.x

File.stream!("ignore/customers.csv")
|> CSV.decode
|> Enum.each(fn([id, nm, csr, sal]) ->
        Customer.changeset(%Customer{},
            %{masterid: id,
              custname: nm,
              csrid: String.to_integer(csr),
              salesid: String.to_integer(sal)})
        |> Repo.insert
    end)

Latest master:

File.stream!("ignore/customers.csv")
|> CSV.decode
|> Enum.each(fn
        {:ok, [id, nm, csr, sal]} ->
            Customer.changeset(%Customer{},
                %{masterid: id,
                  custname: nm,
                  csrid: String.to_integer(csr),
                  salesid: String.to_integer(sal)})
            |> Repo.insert
         {:error, message} ->
            # Whatever you want to do with invalid rows
    end)

Do these work?

from csv.

simonh1000 avatar simonh1000 commented on September 4, 2024

How annoying - i'm sure I tried that. I only needed csv to copy across a database so absolute performance is not a necessity, so I'm going to stick with what i had. But I would recommend you add something like this to the README

from csv.

hopewise avatar hopewise commented on September 4, 2024

Hi,

As I've read the latest sample code above, I see that Enum.each is used, however, I read in the elixir docs:

Note that the functions in the Enum module are eager: they always start the enumeration of the given enumerable. The Stream module allows lazy enumeration of enumerables and provides infinite streams.

So, why not use Stream instead of Enum to iterate through the output of CSV.decode?

from csv.

beatrichartz avatar beatrichartz commented on September 4, 2024

@hopewise CSV.decode works with streams, but at some point you want to start enumeration, otherwise the stream will never be run. This is why you are using Enum.each, which is roughly equivalent to doing Stream.each ... |> Stream.run

from csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.