I basically want the effect of:
str = str.replace('\\n', '\n').replace('\"', '"')...
Some answers for this seem to be 'encode then decode', but that only works in Python2 (I'm using Python3). I've tried the str.encode('utf-8').decode('unicode_escape'), but I've read 'unicode_escape' is deprecated, and it seems to replace non-ASCII characters with unicode code points on output.
A better illustration:
s = '\\"こんにちは\\"'
print(s)
s = s.encode('utf-8').decode('unicode_escape')
print(s)
Output:
\"こんにちは\"
"ããã«ã¡ã¯"
What I want:
"こんにちは"
Edit: (for everyone saying just use .replace()
)
The above is a simple example; more complex could be s = r'こんにちは\\n'
which should convert to r'こんにちは\n'
, for example. If I just replace all instances of r'\\'
with r'\'
and instances of r'\n'
with '\n'
, it won't handle cases where the escape character itself is escaped.
Maybe I could roll my own solution to this - but I prefer to use a language feature, if possible.