I have input and output text files which can contain non-ascii characters. Sometimes I need to escape them and sometimes I need to write the non-ascii characters. Basically if I get "Bürgerhaus" I need to output "B\u00FCrgerhaus". If I get "B\u00FCrgerhaus" I need to output "Bürgerhaus".
One direction goes fine:
>>> s1 = "B\u00FCrgerhaus"
>>> print(s1)
Bürgerhaus
however in the other direction I do not get the expected result ('B\u00FCrgerhaus'):
>>> s2 = "Bürgerhaus"
>>> s2_trans = s2.encode('utf8').decode('unicode_escape')
>>> print(s2_trans)
Bürgerhaus
I read that unicode-escape needs latin-1, I tried to encode it to it, but this did not product a result either. What am I doing wrong?
(PS: Thank you Matthias for reminding me that the conversion in the first example was not necessary.)