Here is the reduced content of my script:
print u"w\xa0p"
which will print "w" character, followed by a nonbreaking space, then "p" to the TTY. However when this script is passed on to a pipeline, e.g.
python script.py | cat
(the exact command on the right of the pipeline is not important, as long as the python command is passed onto a UNIX pipeline), python will complain with this kind of error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 1: ordinal not in range(128)
Can somebody explain why this error happened, and how to mitigate it? Apparently there is a different codec for I guess we could use unidecode package to mitigate the issue at the cost of character precision; but I wonder if we can force python to print the utf-8 characters out anyway if the output pipeline is in use.