3

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?

Community
  • 1
  • 1
user1358
  • 623
  • 2
  • 8
  • 13

1 Answers1

2

Try this code,

#!/usr/bin/python
for i in range(999):
   print i, unichr(i).encode('utf-8')
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42