Consider the following terminal command line
python3 -c 'print("hören")'
In most terminals this prints "hören" (German for "to hear"), in some terminals you get the error
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 1: ordinal not in range(128)
In my Python3 program I don't want that just printing out something can raise an exception like this, instead I'd rather like to output the characters which will not raise an exception.
So my questions is: How can I output in Python3 (unicode) strings while ignoring un-encodable characters?
What I've tried so far
I tried using
sys.stdout.write instead of
I tried encoding the string in byes via
This never raises an exception on print, but even in capable terminals non-ascii characters are replaced by their code point numbers.
I tried using the
decode method with the
bytes=line.encode('utf-8') decoded=bytes.decode('utf-8', 'ignore') print(decoded)
But the problem is not the decoding in the string but the enconding in the print function.
Here some terminals which appear not to be capable of all characters
bash shell inside Emacs on macOS.
Receiving a "printed" string in Applescript via
do shell script, e.g.:
set txt to do shell script "/usr/local/bin/python3 -c \"print('hören')\" "
Update: These terminals all return from
My preferred way is to set PYTHONIOENCODING variable depending on the terminal you are using.
For UTF-8-enabled terminals you can do:
For printing '?'s in ASCII terminals, you can do:
Or even better, if you don't care about the encoding, you should be able to do: