UTF-16 encoding line breaks?

Martin v. Löwis martin at v.loewis.de
Wed Jun 11 17:21:00 EDT 2003


Chris Reedy <creedy at mitretek.org> writes:

>  >>> testfile = codecs.open('testfile.txt', mode='wb', encoding='utf16')
>  >>> testfile.write('abc\r\ndef\r\n')

That's an error: you should write Unicode objects to a file opened
by codecs.open. So use

testfile.write(u'abc\r\ndef\r\n')

instead.

> UnicodeError: UTF-16 decoding error: truncated data

Yes, .readline does not work on a UTF-16 stream. In Python 2.3,
you get

NotImplementedError: .readline() is not implemented for UTF-16

Contributions are welcome.

Regards,
Martin




More information about the Python-list mailing list