csv module and NULL data byte
John Pote
johnpote at jptechnical.co.uk
Thu Mar 1 18:37:54 EST 2018
On 01/03/2018 02:38, Dennis Lee Bieber wrote:
> On Wed, 28 Feb 2018 23:40:41 +0000, John Pote <johnpote at jptechnical.co.uk>
> declaimed the following:
>
>
>> with open( fname, 'rt', encoding='iso-8859-1' ) as csvfile:
> Pardon? Has the CSV module changed in the last year or so?
Python 3.6 docs say csv reader has to be given an iterator that supplies
strings. That requires the file to be opened 'rt' mode as 'rb' returns
bytes objects and the csv reader throws an exception. I did a test to
check and with the encoding='iso-8859-1' the file obj does pass through
all 256 possible byte values without exception.
Interesting point - how are the 'bytes' from the file translated to
Python's unicode strings.
Only doing this for 3.6 so have not checked 2.7 docs.
>
> Last time I read the documentation, it was recommended that the file be
> opened in BINARY mode ("rb").
Using the 'rb' mode results in the helpful exception,
>>python36 csvTest.py
Traceback (most recent call last):
File "csvTest.py", line 25, in <module>
data = list( csvreader )
_csv.Error: iterator should return strings, not bytes (did you open
the file in text mode?)
Good point so thanks anyway,
John
More information about the Python-list
mailing list