Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I escape commas and speech marks in CSV files so they work in Excel?

I'm generating a CSV file (delimited by commas rather than tabs). My users will most likely open the CSV file in Excel by double clicking it. My data may contain commas and speech marks, so I'm escaping those as follows.

Reference, Title, Description 1, "My little title", "My description, which may contain ""speech marks"" and commas." 2, "My other little title", "My other description, which may also contain ""speech marks"" and commas." 

As far as I know that's always been the way to do it. Here's my boggle: when I open this file in Excel 2010 my escaping is not respected. Speech marks appear on the sheet, and the comma causes new columns.

like image 754
centralscru Avatar asked Sep 18 '12 08:09

centralscru


People also ask

How do you handle double quotes and commas in a CSV file?

Since CSV files use the comma character "," to separate columns, values that contain commas must be handled as a special case. These fields are wrapped within double quotation marks. The first double quote signifies the beginning of the column data, and the last double quote marks the end.

How do you escape special characters in CSV?

By default, the escape character is a " (double quote) for CSV-formatted files. If you want to use a different escape character, use the ESCAPE clause of COPY , CREATE EXTERNAL TABLE or gpload to declare a different escape character.

Why do commas mess with CSV files?

This comma breaks the CSV format, since it's interpreted as a new column. I've read up and the most common prescription seems to be replacing that character, or replacing the delimiter, with a new value (e.g. this|that|the, other ).


2 Answers

We eventually found the answer to this.

Excel will only respect the escaping of commas and speech marks if the column value is NOT preceded by a space. So generating the file without spaces like this...

Reference,Title,Description 1,"My little title","My description, which may contain ""speech marks"" and commas." 2,"My other little title","My other description, which may also contain ""speech marks"" and commas." 

... fixed the problem. Hope this helps someone!

like image 141
centralscru Avatar answered Oct 17 '22 03:10

centralscru


Below are the rules if you believe it's random. A utility function can be created on the basis of these rules.

  1. If the value contains a comma, newline or double quote, then the String value should be returned enclosed in double quotes.

  2. Any double quote characters in the value should be escaped with another double quote.

  3. If the value does not contain a comma, newline or double quote, then the String value should be returned unchanged.

like image 40
AlphaBetaGamma Avatar answered Oct 17 '22 03:10

AlphaBetaGamma