docjazither
Hey OGs,
I have a function that loads csv files into my app. When my user used special characters, it failed
they used name like: Zoë
Here is my function:
defp read(csv) do
csv.path
|> Path.expand()
|> File.stream!()
|> CSV.decode(headers: true)
|> Stream.map(fn {:ok, data} -> data end)
|> Stream.reject(&is_nil/1)
|> break_point()
|> Enum.to_list() # It fails here
|> Enum.uniq()
end
Error:
The following arguments were given to CSV.Decoding.Preprocessing.Lines.starts_sequence?/5:
# 1
<<145, 32, 72, 111, 97, 100, 44, 105, 110, 115, 116, 97, 103, 114, 97, 109>>
# 2
"o"
# 3
false
# 4
44
# 5
""
Attempted function clauses (showing 5 out of 5):
defp starts_sequence?(<<34::utf8(), tail::binary()>>, last_token, false, separator, _) when last_token == <<separator::utf8()>>
defp starts_sequence?(<<34::utf8(), tail::binary()>>, "", false, separator, _)
defp starts_sequence?(<<34::utf8(), tail::binary()>>, _, quoted, separator, sequence_start)
defp starts_sequence?(<<head::utf8(), tail::binary()>>, _, quoted, separator, sequence_start)
defp starts_sequence?("", _, quoted, _, sequence_start)
Apparently, it couldn’t recognise this character ë
I wished I could like make a callback for starts_sequence? to return a meaningful error or something like that..
How can I prevent this from happening ?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
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
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Make sure you have the latest version of
csv- version 2.5.0 contains a fix for this error:https://github.com/beatrichartz/csv/pull/107
docjazither
Thanks alot al2o3cr
My leader doesn’t want to upgrade since he’s unsure if it won’t break other things.
Is there a way that I can print out which character is?
al2o3cr
The offending byte is the first one of the binary in the first argument of that error -
145. The preceding character was apparentlyo(the second argument tostarts_sequence?) so a byte with value 145 is not valid UTF8.145 happens to be a left single-quote (
‘) in Windows-1252, which is probably what the creator of the CSV intended.There’s no good way to handle this error apart from upgrading, as the code that was added back in 2.5.0 is the error handling code!
docjazither
Thanks, @al2o3cr
I actually upgraded to 2.5.0 anyway, it does not seem to process all the lines that have offending characters and stops at the line with the first offending one.
Apart from the error handling code, is it possible to retrieve the offending byte?
al2o3cr
AFAIK the only currently available way to deal with malformed UTF8 in
CSV.decodeis to pass the undocumentedreplacementoption - it’s passed down unmodified toCSV.Decoding.Decoderwhere it is documented.Prior to 2.5.0, this wasn’t an option because replacement happens in the lexer way after the
starts_sequence?code in the preprocessor, so bad characters never made it to where they could be replaced.A possible extension to CSV would be to also accept a 1-argument function as
replacement, and provide it with the bad character.jarvism
Thank you for this code!!!
As a newb, seeing an actual implementation of reading the CSV is priceless!!!
The answer to this question is also very helpful!
I’m just not there yet.