1

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.

Rollie
  • 4,391
  • 3
  • 33
  • 55
  • 1
    Per [this](https://bugs.python.org/issue21331) the accepted solution you referenced is deprecated and boils down to a hand rolled regular expression, which is generally not a very high quality option (poor readability, hard to catch all cases, etc) – Rollie May 25 '18 at 07:35
  • Assuming you're disputing the duplicate closure: If the question is the same, it's a duplicate. If the answers are bad, post a better one. Do you think reopening your question would get you better answers? – Aran-Fey May 25 '18 at 07:41

0 Answers0