I have a string. In that string are double backslashes. I want to replace the double backslashes with single backslashes, so that unicode char codes can be parsed correctly.
(Pdb) p fetched_page
'
Chapter 0<\\/span><\\/strong><\\/p>\n
Chapter 0 in \\u201cDreaming in Code\\u201d give a brief description of programming in its early years and how and why programmers are still struggling today...'
Inside of this string, you can see escaped unicode character codes, such as:
\\u201c
I want to turn this into:
\u201c
Attempt 1:
fetched_page.replace('\\\\', '\\')
but this doesn't work -- it searches for quadruple backslashes.
Attempt 2:
fetched_page.replace('\\', '\')
But this results in an end of line error.
Attempt 3:
fetched_page.decode('string_escape')
But this had no effect on the text. All the double backslashes remained as double backslashes.
解决方案
You can try codecs.escape_decode, this should decode the escape sequences.