I used this function successfully with Python 2.4 to alter the encoding
of a set of database records from latin-1 to utf-8, but the same
program raises an exception using Python 2.5. This small example shows
the problem:
import codecs
fo = open('test.dat', 'w')
fo.write('G\xe2teaux')
fo.close()
fi = open("test.dat",'r')
fx = codecs.EncodedFile(fi, 'utf-8', 'latin-1')
astring = fx.readline()
print astring
ustring = unicode(astring, 'utf-8' )
print repr(ustring)
print ustring.encode('latin-1')
print ustring.encode('utf-8')
Python 2.4 gives:
Gâteaux
u'G\xe2teaux'
Gâteaux
Gâteaux
which I believe is correct, while 2.5 produces
Traceback (most recent call last):
File "test_codec.py", line 8, in <module>
astring = fx.readline()
File "C:\Python25\lib\codecs.py", line 709, in readline
data = self.reader.readline()
File "C:\Python25\lib\codecs.py", line 471, in readline
data = self.read(readsize, firstline=True)
File "C:\Python25\lib\codecs.py", line 418, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-3:
invalid data
Is there a genuine problem here, or have I been misusing this function?
--
Regards
David Hughes 1 2905
David Hughes wrote:
I used this function successfully with Python 2.4 to alter the encoding
of a set of database records from latin-1 to utf-8, but the same
program raises an exception using Python 2.5. This small example shows
the problem:
import codecs
fo = open('test.dat', 'w')
fo.write('G\xe2teaux')
fo.close()
fi = open("test.dat",'r')
fx = codecs.EncodedFile(fi, 'utf-8', 'latin-1')
astring = fx.readline()
print astring
ustring = unicode(astring, 'utf-8' )
print repr(ustring)
print ustring.encode('latin-1')
print ustring.encode('utf-8')
Python 2.4 gives:
Gâteaux
u'G\xe2teaux'
Gâteaux
Gâteaux
which I believe is correct, while 2.5 produces
Traceback (most recent call last):
File "test_codec.py", line 8, in <module>
astring = fx.readline()
File "C:\Python25\lib\codecs.py", line 709, in readline
data = self.reader.readline()
File "C:\Python25\lib\codecs.py", line 471, in readline
data = self.read(readsize, firstline=True)
File "C:\Python25\lib\codecs.py", line 418, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-3:
invalid data
Is there a genuine problem here, or have I been misusing this function?
This is indeed a bug in Python 2.5. Fixed in subversion. http://svn.python.org/view/python/tr...52517&view=log
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven Bethard |
last post by:
I just wanted to thank Python for making encodings so easy!
I recently discovered that one of the tools I use stores everything in
UTF-8, and so I was getting some off-by-one errors because I was...
|
by: Eric Brunel |
last post by:
Hi all,
I just found a problem in the xreadlines method/module when used with codecs.open: the codec specified in the open does not seem to be taken into account by xreadlines which also returns...
|
by: Paul Watson |
last post by:
$ python
Python 2.4.1 (#1, May 16 2005, 15:19:29)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import codecs
>>> codecs.lookup('ascii')
(<built-in...
|
by: Giovanni Bajo |
last post by:
Hello,
python24.dll is much bigger than python23.dll. This was discussed already on
the newsgroup, see the thread starting here:...
|
by: Richard Taylor |
last post by:
User-Agent: OSXnews 2.07
Xref: number1.nntp.dca.giganews.com comp.lang.python:437315
Hi
I am trying to use py2app (http://undefined.org/python/) to package a
gnome-python application...
|
by: aurora |
last post by:
I have some unicode string with some characters encode using python
notation like '\n' for LF. I need to convert that to the actual LF
character. There is a 'unicode_escape' codec that seems to...
|
by: Neil Cerutti |
last post by:
Perhaps I'm just bad at searching for bugs, but anyhow, I wanted
to know what you all thought about the following behavior.
A quick search of pydev archives yielded a nice wrapper to apply
to...
|
by: jmalone |
last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has
AIX and does not want to install Python). The python script is pretty
simple (the only things it imports are sys and socket)....
|
by: yrogirg |
last post by:
Actually, I need utf-8 to utf-8 encoding which would change the text
to another keyboard layout (e.g. from english to russian ghbdtn ->
ĐŇÉ×ĹÔ) and would not affect other symbols.
I`m totally...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |