Possible Duplicate:
Setting the correct encoding when piping stdout in python
The following runs in the python shell (2.7.3) as expected
for i in range(999):
print i, unichr(i)
saving it in a file (asd.py), and running in the shell
$ ./asd.py
works also, but
$ ./asd.py > asd.txt
gives:
Traceback (most recent call last):
File "./asd.py", line 3, in <module>
print i, unichr(i)
UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
Why is that? How to work around it?