Read file that starts with '\xff\xfe'

Gerhard Häring gh at ghaering.de
Mon Sep 8 10:40:31 EDT 2003


Bob Gailer wrote:
> [...] UniCodeError: UTF-16 decoding error: truncated data

If I remove the last character of the example line you posted, I can 
sucessfully convert it to a Unicode string:

 >>> s = '\xff\xfe"\x00T\x00a\x00s\x00k\x00 
\x00S\x00c\x00h\x00e\x00d\x00u\x00l\x00e\x00r\x00 
\x00S\x00e\x00r\x00v\x00i\x00c\x00e\x00"\x00\r\x00\n'
 >>> unicode(s, "utf-16")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
UnicodeDecodeError: 'utf16' codec can't decode byte 0xa in position 52: 
truncate
d data
 >>> unicode(s[:-1], "utf-16")
u'"Task Scheduler Service"\r'
 >>>

I'm using Python 2.3, which apparently gives more useful encoding errors 
(including the position of the error).

-- Gerhard





More information about the Python-list mailing list