Hi,
I am using Python 2.6.1 and am having utf-8 related problem with my code. This problem is reproducible with this code:
# -*- coding: utf-8 -*-
import os, sys
import string, time
import codecs, re
bDATA='"Domenick Lombardozzi","Eddie Marsan","Isaach De Bankolé","John Hawkes"'
print (bDATA)
fileObj = codecs.open("btvresp1.txt", "r", "utf-8")
data = fileObj.read()
print (data)
The first print of bDATA
works just fine. However, if the same data is in the file btcresp1.txt file, python complains as follows:
cat btvresp2.txt
"Domenick Lombardozzi","Eddie Marsan","Isaach De Bankol?","John Hawkes"
python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # -*- coding: utf-8 -*-
...
>>> import os, sys
>>> import string, time
>>> import codecs, re
>>> bDATA='"Domenick Lombardozzi","Eddie Marsan","Isaach De Bankol","John Hawkes"'
>>> print (bDATA)
"Domenick Lombardozzi","Eddie Marsan","Isaach De Bankol","John Hawkes"
>>> fileObj = codecs.open("btvresp2.txt", "r", "utf-8")
>>> data = fileObj.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py", line 666, in read
return self.reader.read(size)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py", line 472, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 55-57: invalid data
I am not sure as to why the same data when read from a file causes problems. Can someone shed light as to why this behavior and how I can resolve this?
Thanks in advance!