codecs latin1 unicode standard output file

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


Hi,

"Michael Hudson" <mwh at python.net> schrieb im Newsbeitrag
news:m3r7z69xib.fsf at pc150.maths.bris.ac.uk...
> "Marko Faldix" <marko.faldix.tudisweck at mplusr.de> writes:
>
> > Hello,
> >
> > with Python 2.3 I can write umlauts (a,o,u umlaut) to a file with this
piece
> > of code:
> >
> >     import codecs
> >
> >     f = codecs.open("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
to
> > write this to standard output so that I can use same code to produce
output
> > lines on console or to use this to pipe into file. It was possible
before
> > 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 klotentest1.py:

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

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

Calling this on command line:

klotentest1.py

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.py > klotentest1.txt

This fails:
Traceback (most recent call last):
File "C:\home\marko\moeller_port\moeller_port_exec_svn\klotentest1.py", 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.py > 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