问题
I am trying to read a CSV file containing a line like following:
test,"test,"test,test,test,test
There is a problems with the quotation marks (There are six fields, but they are retrieved as five fields, as "test,"test is read as a single field).
I have tried modifying the entry as follows, but I still can't retrieve the quotation mark:
test,""test,""test,test,test,test # quotation marks disappear when the entry is read.
test,\"test,\"test,test,test,test # backslashes are also retrieved; escaping doesn't seem to work.
I'm reading CSV file this way:
info_source = csv.reader(open('.info.csv'), skipinitialspace=True)
for row in ling_info_source:
data = row[1].strip()
...
回答1:
By default "
is the quoting character of Python's csv
module. Use
csv.reader(open('.info.csv'), skipinitialspace=True, quotechar=None)
to disable this default. The example you gave will result in the record
['test', '"test', '"test', 'test', 'test', 'test']
回答2:
You can add the quoting=csv.QUOTE_NONE
argument to reader()
来源:https://stackoverflow.com/questions/9209546/python-csv-field-containing-quotation-mark-at-the-beginning