Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly escape a double quote in CSV

Tags:

escaping

csv

I have a line like this in my CSV:

"Samsung U600 24"","10000003409","1","10000003427"

Quote next to 24 is used to express inches, while the quote just next to that quote closes the field. I'm reading the line with fgetcsv but the parser makes a mistake and reads the value as:

Samsung U600 24",10000003409"

I tried putting a backslash before the inches quote, but then I just get a backslash in the name:

Samsung U600 24\"

Is there a way to properly escape this in the CSV, so that the value would be Samsung U600 24" , or do I have to regex it in the processor?

like image 724
srgb Avatar asked Oct 20 '22 16:10

srgb


People also ask

How do you escape a double quote 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.

How do you handle quotes in a CSV file?

So quote characters are used in CSV files when the text within a field also includes a comma and could be confused as being the reserved comma delimiter for the next field. Quote characters indicate the start and end of a block of text where any comma characters can be ignored.


1 Answers

Use 2 quotes:

"Samsung U600 24"""
like image 439
user4035 Avatar answered Oct 23 '22 06:10

user4035