[issue40463] csv.reader split error

Eric V. Smith report at bugs.python.org
Fri May 1 03:40:16 EDT 2020


Eric V. Smith <eric at trueblade.com> added the comment:

You should tell us what you're seeing, and what you're expecting.

I'm adding the rest of this not because it solves your problem, but because it might help you or someone else troubleshoot this further.

Here's a simpler reproducer:

import csv
lst = ['"A,"h"e, ","E","DC"']

csv_list = csv.reader(lst)
for idx, col in enumerate(next(csv_list)):
    print(idx, repr(col))

Which produces:
0 'A,h"e'
1 ' "'
2 'E'
3 'DC'

Although true to its word, this is using the default dialect='excel', and my version of Excel gives these same 4 columns, including the space starting the second column.

Dropping the space after the "e," gives 3 columns:

lst = ['"A,"h"e,","E","DC"']

Produces:
0 'A,h"e'
1 ',E"'
2 'DC'

Again, this is exactly what Excel gives, as odd as it seems.

It might be worth playing around with the dialect parameters to see if you can achieve what you want. In your example:
delimiter=',', quotechar='"'
are the default values for the "excel" dialect, which is why I dropped them above.

----------
nosy: +eric.smith

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40463>
_______________________________________


More information about the Python-bugs-list mailing list