zwippie

zwippie

I want to dump some data to a CSV file where each column value is separated with a semicolon and wrapped inside double quotes. The semicolon is no problem, but the double quotes are.

This does not work, no double quotes:

NimbleCSV.define(MyParser, separator: ";", escape: "\"")
data = [["firstname", "lastname"], ["Foo", "Bar"]]
IO.iodata_to_binary MyParser.dump_to_iodata(data)

That will return:

"firstname;lastname\nFoo;Bar\n"

But i want this:

"\"firstname\";\"lastname\"\n\"Foo\";\"Bar\""

How to wrap each column with double quotes?

Showing Posts 1 to 5

idi527

idi527

:waving_hand:

Would

NimbleCSV.define(MyParser, separator: ";", escape: "\"")
data = [["firstname", "lastname"], ["Foo", "Bar"]]

data
|> Enum.map(fn row ->
  Enum.map(row, fn value -> [?", value, ?"] end)
end)
|> MyParser.dump_to_iodata()
|> IO.iodata_to_binary 

work?

As an example

iex(1)> ?"
34
iex(2)> [?", "hello", ?"]
[34, "hello", 34]
iex(3)> IO.iodata_to_binary [?", "hello", ?"]
"\"hello\""

I useda an iolist here since I suspect it might be a bit more efficient than "\"#{value}\"".

peerreynders

peerreynders

iex(1)> data = [["\"firstname\"", "\"lastname\""], ["\"Foo\"", "\"Bar\""]]
[["\"firstname\"", "\"lastname\""], ["\"Foo\"", "\"Bar\""]]
iex(2)> IO.iodata_to_binary MyParser.dump_to_iodata(data)
"\"\"\"firstname\"\"\";\"\"\"lastname\"\"\"\n\"\"\"Foo\"\"\";\"\"\"Bar\"\"\"\n"
iex(3)> 

… so not exactly.

rfc4180

  1. Each field may or may not be enclosed in double quotes (however
    some programs, such as Microsoft Excel, do not use double quotes
    at all). If fields are not enclosed with double quotes, then
    double quotes may not appear inside the fields.

NimbleCSV has no problem consuming

"\"firstname\";\"lastname\"\n\"Foo\";\"Bar\""

it just doesn’t seem interested in producing it (likely to avoid unnecessarily bloating the output).

iex(1)> MyParser.parse_string("\"firstname\";\"lastname\"\n\"Foo\";\"Bar\"",[headers: false])
[["firstname", "lastname"], ["Foo", "Bar"]]
iex(2)> 
  1. Fields containing line breaks (CRLF), double quotes, and commas
    should be enclosed in double-quotes.

NimbleCSV will only use the double quotes when absolutely necessary, e.g.:

iex(1)> data = [["firstname", "lastname"], ["Fo\no", "Bar"]]
[["firstname", "lastname"], ["Fo\no", "Bar"]]
iex(2)> IO.iodata_to_binary MyParser.dump_to_iodata(data)   
"firstname;lastname\n\"Fo\no\";Bar\n"
iex(3)> data = [["firstname", "lastname"], ["Fo\"o", "Bar"]]
[["firstname", "lastname"], ["Fo\"o", "Bar"]]
iex(4)> IO.iodata_to_binary MyParser.dump_to_iodata(data)   
"firstname;lastname\n\"Fo\"\"o\";Bar\n"
iex(5)> 

that last example also demonstrating

  1. If double-quotes are used to enclose fields, then a double-quote
    appearing inside a field must be escaped by preceding it with
    another double quote.
zwippie

zwippie OP

Thank you for your great explanation! Considering rfc4180, it all makes sense of course. I would welcome a force_escape option for NimbleCSV though.

Now I’m going to find out how many clients rely on our current (quoted) CSV output format.

peerreynders

peerreynders

#
# relies on implementation details !!!
# may have undesirable side effects !!!
# USE AT YOUR OWN RISK !!!
#

defmodule Nimble do

  @separator ";"
  @line_separator "\n"
  @escape "\""

  # http://elixir-recipes.github.io/strings/list-with-alphabet/
  @lower for n <- ?a..?z, do: << n :: utf8 >> 
  @upper for n <- ?A..?Z, do: << n :: utf8 >>
  @numeric for n <- ?0..?9, do: << n :: utf8 >>
  @symbol for n <- [?_,?-,?+,?/,?*], do: << n :: utf8 >>

  @reserved [@escape, @separator, @line_separator] ++ @lower ++ @upper ++ @numeric ++ @symbol

  def make_options do
    [
      separator: @separator,
      escape: @escape,
      line_separator: @line_separator,
      reserved: @reserved
    ]
  end

end

NimbleCSV.define(MyParser, Nimble.make_options())
iex(1)> data = [["firstname", "lastname"], ["Foo", "Bar"]]
[["firstname", "lastname"], ["Foo", "Bar"]]
iex(2)> IO.iodata_to_binary MyParser.dump_to_iodata(data)
"\"firstname\";\"lastname\"\n\"Foo\";\"Bar\"\n"
iex(3)> 

… and that still doesn’t cover an empty string.

This seems like a more reasonable approach:

defmodule Nimble do

  @separator ?;
  @line_separator ?\n
  @escape ?"

  def make_options do
    [
      separator: << @separator :: utf8 >>,
      line_separator: << @line_separator :: utf8 >>,
      escape: << @escape :: utf8 >>
    ]
  end

  defp escape_entry([@escape, value, @escape] = entry) when is_binary(value),
    do: entry
  defp escape_entry(value) when is_binary(value),
    do: [@escape, value, @escape]

  defp escape_row([], ys),
    do: :lists.reverse(ys)
  defp escape_row([entry, sep | xs], ys),
    do: escape_row(xs, [sep, escape_entry(entry)|ys])

  def escape_row(row),
    do: escape_row(row,[])

  defp escape_list([], ys),
    do: :lists.reverse(ys)
  defp escape_list([row | xs], ys),
    do: escape_list(xs, [escape_row(row)|ys])

  def escape_list(list),
    do: escape_list(list,[])

end

NimbleCSV.define(MyParser, Nimble.make_options())
iex(1)> data = [["firstname", "lastname"], ["Fo;o", "Bar"]]
[["firstname", "lastname"], ["Fo;o", "Bar"]]
iex(2)> IO.iodata_to_binary(Nimble.escape_list(MyParser.dump_to_iodata(data)))
"\"firstname\";\"lastname\"\n\"Fo;o\";\"Bar\"\n"
iex(3)> data = [["firstname", "lastname"], ["", "Bar"]]                       
[["firstname", "lastname"], ["", "Bar"]]
iex(4)> IO.iodata_to_binary(Nimble.escape_list(MyParser.dump_to_iodata(data)))
"\"firstname\";\"lastname\"\n\"\";\"Bar\"\n"
iex(5)> 
nallwhy

nallwhy

Simple implementation

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews