3

I am trying to read a CSV file containing a line like following:

test,"test,"test,test,test,test

There is a problem 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()
    ...
simleo
  • 2,775
  • 22
  • 23
David
  • 595
  • 1
  • 8
  • 19

2 Answers2

3

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']
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • The problem with this solution is that I can't read cases such as: [test, "test, "test, "commaToRead,commaToRead", test, test]. For this case I should read: [test]["test]["test]["commaToRead,commaToRead"][test][test]. Is it possible? – David Feb 09 '12 at 13:28
  • @David: That's ambiguous. How is the parser supposed to know which double quotes are meant for quoting and which aren't? – Sven Marnach Feb 09 '12 at 13:45
  • I thought I should add this because it's something I had to test to clarify: the quoting character only applies if it appears right after the delimiter. That is to say, the quoting character must be the first character in the new delimited section in order to nullify any delimiters within the text. – Zhouster Jul 30 '14 at 01:32
  • i think parser should see that in `"test, "test,` quotation mark is followed by `test` and in `"commaToRead,commaToRead",` quotation mark is followed by comma – Winand Dec 19 '15 at 10:43
3

You can add the quoting=csv.QUOTE_NONE argument to reader()

Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • While using `quotechar=None` as in my answer works fine, `quoting=csv.QUOTE_NONE` seems to be the documented way to disable quoting in the reader. – Sven Marnach Feb 09 '12 at 11:21