1

There are a lot of variants of the CSV "standard" (or lack thereof). I've never personally see any that use an escape character (like \) instead of surrounding each field with double quotes. Instead of foo,bar,"foo,bar" it would be foo,bar,foo\,bar.

This would be handy for situations where a file needs to manually inspected or edited by hand. When counting commas to find the right field, it seems that it would be easier to tell which ones were not field separators if they escaped instead of quoted.

I don't see how it would make a difference from a parsing perspective, though.

Why quote instead of escape?

poke
  • 570
  • 4
  • 12
  • For text which doesn't contain quotation marks, it reduces the problem to simply searching the text to see if it includes any commas and enclosing in quotation marks if it does, which is simpler and faster than either creating a new string to insert the escaps into or writing it out in parts to emit the escape key when required. – James McLeod Jul 20 '13 at 15:14
  • 2
    I'm not sure it would be easier: For example, in `foo,bar,foo\\,bar`, the last comma would be a field separator. – Heinzi Jul 20 '13 at 17:12
  • You really need both. Escaping is shorter. Quoting easier for "humans" to read (though as you point out for some jobs escaping makes it easier for simpler editors to count). Even with Quotes you still need escaping otherwise how do you put `"` into the cell. Quotes also allow you to put new lines more read-ably. When writting the parser if you are going to implement one you may as well implement both it does not add much complexity. – Martin York Jul 21 '13 at 17:27

2 Answers2

1

Your question includes the answer, when you wrote "I don't see how it would make a difference from a parsing perspective, though"

There is no compelling reason, it just is. Csv is a data format, so the main goal is to be parseable.

Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
0

Chicken and egg.

If I was defining a spec, I'd escape for the reasons that you state. But any CSV parsing tool that you encounter (including Excel, sqloader, etc) is almost certain to use quoting, not escaping. So if you want to produce it, you need to produce it quoted. If you want to consume it, it is safe to assume that whoever generates it will quote.

btilly
  • 18,250
  • 1
  • 49
  • 75