[Tutor] appending to a utf-16 encoded text file

Jerry Hill malaclypse2 at gmail.com
Tue Oct 21 19:35:37 CEST 2008


On Tue, Oct 21, 2008 at 9:52 AM, Tim Brown <tocb2003 at yahoo.co.uk> wrote:
> Hi,
> I'm trying to create and append unicode strings to a utf-16 text file.
> The best I could come up with was to use codecs.open() with an
> encoding of 'utf-16' but when I do an append I get another UTF16 BOM
> put into the file which other programs do not expect to see :-(
> Is there some way to stop codecs from doing this or is there a better
> way to create and add data to a utf-16 text file?

That appears to be bug 1701389 (http://bugs.python.org/issue1701389),
which was closed as "Won't Fix" last year.

You should be able to work around it by doing the encoding yourself, like this:

my_file = open("my_utf16_file.txt", "ab")
my_file.write(my_unicode_str.encode("utf16"))

-- 
Jerry


More information about the Tutor mailing list