Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python CSV Parsing, Escaped Quote Character

I am trying to parse a CSV file using the csv.reader, my data is separated by commas and each value starts and ends with quotation marks. Example:

"This is some data", "New data", "More \"data\" here", "test"

My problem is with the third value, the data I get which has quotation marks within it has an escape character to show it is part of the data. The python CSV reader does not use this escape character so it results in incorrect parsing.

I tried code like below:

    with open(filepath) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',', quotechar='\\"')

But I get an error complaining the quotechar is not 1 character.

My current solution is just to replace all characters \" characters with a single quote ' before parsing with csv.reader - however, I would like to know if there is a better way without modifying the original data.

like image 986
KillerKode Avatar asked Apr 02 '19 08:04

KillerKode


People also ask

How do you escape quotes 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 the hawq load control file to declare a different escape character.

What is a Quotechar in CSV Python?

quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote ( ' " ' ). escapechar specifies the character used to escape the delimiter character, in case quotes aren't used.

How do I put quotes in a CSV file?

CSV Format 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 I remove quotes from a CSV file in Python?

Using the csv. reader() Function to Remove Quotation from CSV in Python.


1 Answers

The issue here is that you need to define an escapechar, so that the csv reader knows to treat \" as ".

csv.reader(csv_file, quotechar='"', delimiter=',', escapechar='\\')
like image 96
Greg Avatar answered Oct 06 '22 13:10

Greg