[I18n-sig] Transparent Encoding

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Fri, 18 May 2001 23:05:09 +0200


> What's a better idiom for
> 
> stream = codecs.EncodeFile(fileobj, "unicode-internal", "utf-8")
> 
> I want to a writable fileobj in a transparent UTF-8 encoder? 

Is fileobj already given as open, or do you have a filename for it?
If the latter, just do

stream = codecs.open(filename, "w", encoding="utf-8")

If the former, do

writer = codecs.lookup("utf-8")[3]
# or
# enc, dec, reader, writer = codecs.lookup("utf-8")

stream = writer(fileobj)

An EncodedFile is not suitable since it has byte strings on both ends,
and Unicode strings only inside.

Regards,
Martin