dashbitco / nimble_csv

A simple and fast CSV parsing and dumping library for Elixir
https://hexdocs.pm/nimble_csv
772 stars 51 forks source link

Usage with csv files that contain escaped double quote entries #60

Closed neenjaw closed 3 years ago

neenjaw commented 3 years ago

Hello, I am a little unclear from the documentation how to use this library with a CSV file that contains an entry with already escaped double quote:

testfile:

1,"Hello \"World\""

test.exs:

NimbleCSV.define(MyParser, separator: ",", escape: ~S'"')

stdio = IO.stream(:stdio, :line)

stdio
|> Stream.map(&String.trim/1)
|> MyParser.parse_stream()
|> Enum.to_list()

I would expect this to succeed with [[1, "Hello \"World\""]], but I encounter this error:

➜ cat testfile | mix run test.exs
** (NimbleCSV.ParseError) unexpected escape character " in "Hello \\\"World\\\"\""
    deps/nimble_csv/lib/nimble_csv.ex:485: MyParser.escape/6
    deps/nimble_csv/lib/nimble_csv.ex:355: anonymous fn/4 in MyParser.parse_stream/2
    (elixir 1.10.4) lib/stream.ex:902: Stream.do_transform_user/6
    (elixir 1.10.4) lib/enum.ex:3383: Enum.reverse/1
    (elixir 1.10.4) lib/enum.ex:2984: Enum.to_list/1
josevalim commented 3 years ago

In CSV, the way to escape the escape character is by having it appear twice. So the format that you have written is not really supported. It will work if you rewrite it as 1,"Hello ""World""" though.

neenjaw commented 3 years ago

Okay, good to know! Thanks