Unicodeencodeerror: 'ascii' Codec Can't Encode Character U'\u03c0'
I am trying to extract the attribute value Body from row element in pi.xml. cat pi.xml Copy
But if your script explicitly encodes its output, such as:
printu'somestring'.encode('utf8')
this method won't work. However, scripts should just print Unicode and let the terminal decide the encoding, as in:
printu'somestring'
Python will automatically encode for UTF-8 if the console is configured for UTF-8.
For your redirection case, Python doesn't know what encoding to use when printing Unicode, so defaults to ascii
. Since redirection is a shell function, leave specifying the encoding to the shell using:
PYTHONIOENCODING=utf8 python pi.py >> py.txt.
This leaves the option open to use other encodings without modifying the script.
Post a Comment for "Unicodeencodeerror: 'ascii' Codec Can't Encode Character U'\u03c0'"