Unicode problem

Rehceb Rotkiv rehceb at no.spam.plz
Sat Apr 7 14:09:30 EDT 2007


Please have a look at this little script:

#!/usr/bin/python
import sys
import codecs
fileHandle = codecs.open(sys.argv[1], 'r', 'utf-8')
fileString = fileHandle.read()
print fileString

if I call it from a Bash shell like this

$ ./test.py testfile.utf8.txt

it works just fine, but when I try to pipe the output to another process 
("|") or into a file (">"), e.g. like this

$ ./test.py testfile.utf8.txt | cat

I get an error:

Traceback (most recent call last):
  File "./test.py", line 6, in ?
    print fileString
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in 
position 538: ordinal not in range(128)

I absolutely don't know what's the problem here, can you help?

Thanks,
Rehceb



More information about the Python-list mailing list