[Python-Dev] File encodings

M.-A. Lemburg mal at egenix.com
Wed Dec 1 10:23:16 CET 2004

Gustavo Niemeyer wrote:
> [...]
>>The idiom presented by Bob is the right way to go: wrap
>>sys.stdout with a StreamWriter.
> I don't see that as a good solution, since every Python software
> that is internationalizaed will have do figure out this wrapping,
> introducing extra overhead unnecessarily.

I don't see any unnecessary overhead and using the wrappers
is really easy, e.g.:

# Application uses Latin-1 for I/O, terminal uses UTF-8
import codecs, sys

# Make stdout translate Latin-1 output into UTF-8 output
sys.stdout = codecs.EncodedFile(sys.stdout, 'latin-1', 'utf-8')

# Have stdin translate Latin-1 input into UTF-8 input
sys.stdin = codecs.EncodedFile(sys.stdin, 'utf-8', 'latin-1')

We should probably extend the support in StreamRecoder (which
is used by the above EncodedFile helper) to also support Unicode
input to .write() and have a special codec 'unicode' that converts
Unicode to Unicode, so that you can request the EncodedFile object
to return Unicode for .read().

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Dec 01 2004)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the Python-Dev mailing list