Below there is a function eating all strings and convert it to Unicode. It works fine. But is it the best way to do so? Is there maybe already an existing function for it?
The reason was a German Windows version which prints German ä ö ü - error-messages which are retrieved from popen/communicate etc. There must have been someone before me with the same issue.
# python2.7
def all_eating_unicode_converter(input_string):
test1 = 'äöü'
test2 = b'\xe4\xf6\xfc'
test3 = u'äöü'
"""
Converts every input to unicode. tested with testcases abouve!
:param input_string: may be a string or unicode or bytes.
:return: returns unicode
"""
l.debug("type: %s\n message:%s" % (type(input_string), input_string))
if type(input_string) is not unicode:
# i don't need to decode unicode. because it already is!
output_string = input_string.decode("utf-8") # converts bytes to unicode
else:
output_string = input_string
return output_string
test2
:b'\xe4\xf6\xfc'.decode("utf-8") -> UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 0: invalid continuation byte
. \$\endgroup\$b'\xc3\xa4\xc3\xb6\xc3\xbc'
instead. \$\endgroup\$