3

I am using python's csv module and using quotes to contain the field that contains the separator.

The file a.txt looks like the following:

ab, ",cd"

And i get the following that indicates that it did not escape the comma in double quotes.

In [27]: with open('a.txt', newline='') as csvfile:
    ...:     reader = csv.reader(csvfile, delimiter=',')
    ...:     for row in reader:
    ...:         if row:
    ...:             print(row)
    ...:
['ab', ' "', 'cd"']
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Captain Jack sparrow
  • 959
  • 1
  • 13
  • 28
  • The space is throwing off the csv module's parsing. With `ab,",cd"` it works as expected. What's producing this bunk csv data? – wim Sep 25 '19 at 20:33

2 Answers2

3

As @wim pointed out in a comment, this is caused by the space character after the comma.

If you configure the reader to skip the space after the delimiter, then it will work:

>>> print(list(csv.reader(io.StringIO('ab, ",cd"'), skipinitialspace=True)))
[['ab', ',cd']]

BTW, see the documentation for other CSV reader options.

zvone
  • 18,045
  • 3
  • 49
  • 77
  • 1
    What happens is that the csv module considers the quote _right after the separator_. If there's something before that (space or something else), the quote is considered like being part of the data. your answer allows to fix this – Jean-François Fabre Sep 25 '19 at 20:49
1

It's not broken, it's the expected behaviour.

What happens is that the csv module only considers the quote as a quote if it's right after the separator.

If there's something before that (space or something else), the quote is considered like being part of the data.

zvone answer explains how to workaround that (in the case of a space only)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219