[issue4847] csv fails when file is opened in binary mode

John Machin report at bugs.python.org
Mon Mar 9 07:49:11 CET 2009


John Machin <sjmachin at users.sourceforge.net> added the comment:

... and it looks like Option 2 might already *almost* be in place.
Continuing with the previous example (book1.csv has embedded lone LFs):

C:\devel\csv>\python30\python -c "import csv;
print(repr(list(csv.reader(open('book1.csv','rt', encoding='ascii')))))"
[['Field1', 'Field 2 has a\nvery long\nheading', 'Field3'], ['1.11',
'2.22', '3.33']]

Looks good. However consider book2.csv which has embedded CRLFs:
C:\devel\csv>\python30\python -c "print(repr(open('book2.csv',
'rb').read()))"
b'Field1,"Field 2 has a\r\nvery
long\r\nheading",Field3\r\n1.11,2.22,3.33\r\n'

This gives:
C:\devel\csv>\python30\python -c "import csv;
print(repr(list(csv.reader(open('book2.csv','rt', encoding='ascii')))))"
[['Field1', 'Field 2 has a\nvery long\nheading', 'Field3'], ['1.11',
'2.22', '3.33']]

Not good. It should preserve ALL characters in the field.

----------
message_count: 11.0 -> 12.0
versions: +Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4847>
_______________________________________


More information about the Python-bugs-list mailing list