Comments (6)
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.
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.
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.
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.
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.
@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)
- Encode Formulas to prevent CSV injection HOT 1
- `headers: true` option doesn't work anymore in version 3.* HOT 5
- Allow parsing of other file encodings (`utf16`, `utf32`, `latin1`) HOT 1
- v3.0.1 module documentation missing on hexdocs.pm HOT 3
- final escaped field value lost when trailing newline is absent HOT 4
- Bug: duplicate error lines after upgrading to v3.0 HOT 2
- Dialyzer fails when using `escape_max_lines` option HOT 1
- Bug: argument error for escape_character HOT 2
- Dialyzer error for decode and decode! HOT 3
- Dealing with keep list intact HOT 2
- Ability to exclude column by name or data type from `escape_formulas`
- Is it possible to get values as nil if the column doesn't have a value when `headers: true`?
- How to export utf8 file with bom?
- CSV.decode! add additional \r\n to double quoted value containing \n HOT 2
- Need to manually strip newlines HOT 1
- Changelog is behind hex.pm versions HOT 1
- Line number is wrong in error message when headers: true HOT 3
- Missing key exception when raising StrayQuoteError HOT 2
- Unable to generate " HOT 2
- Unable to properly enclose cells with double quotes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv.