I am having problems with subprocesses in Python which return unicode characters, especially the German ü, ä, ö characters.
My script basically wants to open a subprocess, which returns some strings with the stdout.read()
function. Some of those strings may contain unicode characters, but it is not always known if and where those characters are. So the output has to be decoded (or encoded?) somehow to correctly display the string.
A byte-object is not possible for me to work with.
The following code displays in short what I try to do, but fails to decode the string, hence the "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 12: invalid start byte" Error-Message:
import subprocess
command_array = ['echo', 'string_with_ü_ä_ö']
command = subprocess.Popen(command_array, stdout=subprocess.PIPE, shell=True)
command_output = command.stdout.read()
command_output = command_output.decode()
print(command_output)
I feel that there has to be some trivial solution to this, which I failed to find anywhere. Is there any way to correctly return those unicode characters in a string?
I am using Python 3.6.3, and the above script runs on Windows. A version which works under Linux as well will be equally appreciated!