the stupid encoding problem to stdout

Sérgio Monteiro Basto sergiomb at sapo.pt
Wed Jun 8 22:18:37 EDT 2011


hi,
cat test.py 
#!/usr/bin/env python
#-*- coding: utf-8 -*-
u = u'moçambique'
print u.encode("utf-8")
print u

chmod +x test.py
./test.py
moçambique
moçambique

./test.py > output.txt
Traceback (most recent call last):
  File "./test.py", line 5, in <module>
    print u
UnicodeEncodeError: 'ascii' codec can't encode character 
u'\xe7' in position 2: ordinal not in range(128)

in python 2.7 
how I explain to python to send the same thing to stdout and 
the file output.txt ?

Don't seems logic, when send things to a file the beaviour 
change.

Thanks,
Sérgio M. B. 



More information about the Python-list mailing list