[I18n-sig] Transparent Encoding

Paul Prescod paulp@ActiveState.com
Fri, 18 May 2001 15:01:57 -0700

"Martin v. Loewis" wrote:
> An EncodedFile is not suitable since it has byte strings on both ends,
> and Unicode strings only inside.

EncodedFile seems to work as I ask if I pass it the encoding name as
"unicode-internal". Furthermore, code that does that is much simpler
than code that looks up the codec manually. I'm not a big fan of those
codec tuples.


writer = codecs.lookup("utf-8")[3]
stream = writer(fileobj)


codecs.EncodedFile(fileobj, None, "utf-8")

As I understand it, you can almost always go without looking up the
encoder tuple thanks to the .encode method. And you can almost always go
without looking up the decoder, thanks to the .decode method. This
EncodedFile convention would allow most common cases of wrapping Unicode
to avoid looking up the tuple also.
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook