codecs latin1 unicode standard output file

Marko Faldix marko.faldix.tudisweck at
Mon Dec 15 12:26:12 CET 2003


"Michael Hudson" <mwh at> schrieb im Newsbeitrag
news:m3r7z69xib.fsf at
> "Marko Faldix" <marko.faldix.tudisweck at> writes:
> > Hello,
> >
> > with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this
> > of code:
> >
> >     import codecs
> >
> >     f ="klotentest.txt", "w", "latin-1")
> >     print >>f, unicode("My umlauts are ä, ö, ü", "latin-1")
> >
> >
> > This works fine. This is not exactly what I wanted to have. I would like
> > write this to standard output so that I can use same code to produce
> > lines on console or to use this to pipe into file. It was possible
> > Python 2.3. Isn't possible anymore with same code?
> If your locale is setup up in an appropriate way, you should be able
> to print latin-1 characters to stdout without any intervention at all.
> If that doesn't work, we need more details.
> Cheers,
> mwh

I try to describe. It's a Window machine with Python 2.3.2 installed. Using
command line (cmd). Put these lines of code in a file called

# -*- coding: iso-8859-1 -*-

print unicode("My umlauts are ä, ö, ü", "latin-1")
print "My umlauts are ä, ö, ü"

Calling this on command line:

Indeed, result of first print is as desired, result of second print delivers
strange letters but no error.
Now I call this on command line: > klotentest1.txt

This fails:
Traceback (most recent call last):
File "C:\home\marko\moeller_port\moeller_port_exec_svn\", line
3, in ?
    print unicode("My umlauts are õ, ÷, ³", "latin-1")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position
15: ordinal not in range(128)

( By the way: error result is same if I call it this way: python > klotentest1.txt )

In my point of view python shouldn't act in different ways whether result is
piped to file or not.

Marko Faldix

More information about the Python-list mailing list