Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a double backslash with a single backslash in python?

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 '<p style="text-align:center;" align="center"><strong><span style="font-family:\'Times New Roman\', serif;font-size:115%;">Chapter 0<\\/span><\\/strong><\\/p>\n<p><span style="font-family:\'Times New Roman\', serif;font-size:115%;">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.

like image 638
zzz Avatar asked Jul 19 '11 18:07

zzz


People also ask

How do you do a single backslash in Python?

Learn More. In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you replace a backslash?

To replace all backslashes in a string:Call the replaceAll() method, passing it a string containing two backslashes as the first parameter and the replacement string as the second. The replaceAll method will return a new string with all backslashes replaced by the provided replacement.

How do you change a double backslash to a single backslash in Java?

myString. replace("\\\\", "\\");


1 Answers

You can try codecs.escape_decode, this should decode the escape sequences.

like image 166
schlamar Avatar answered Sep 20 '22 20:09

schlamar