Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python replace single backslash with double backslash

In python, I am trying to replace a single backslash ("\") with a double backslash("\"). I have the following code:

directory = string.replace("C:\Users\Josh\Desktop\20130216", "\", "\\") 

However, this gives an error message saying it doesn't like the double backslash. Can anyone help?

like image 944
Josh Wood Avatar asked Jun 26 '13 17:06

Josh Wood


People also ask

How do you replace one backslash in Python?

Just use . replace() twice! first operation creates ** for every \ and second operation escapes the first slash, replacing ** with a single \ .

How do you do a double backslash in Python?

The division result of two numbers can be an integer or a floating-point number. In python version 3+, both the single slash (/) operator and the double slash (//) operator are used to get the division result containing the floating-point value.

How do I change backslash to forward slash in Python?

The correct way would be s. replace('/', '\\') . Right now when it's running it will just give you \f which is a linefeed 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.


2 Answers

No need to use str.replace or string.replace here, just convert that string to a raw string:

>>> strs = r"C:\Users\Josh\Desktop\20130216"            ^            |        notice the 'r' 

Below is the repr version of the above string, that's why you're seeing \\ here. But, in fact the actual string contains just '\' not \\.

>>> strs 'C:\\Users\\Josh\\Desktop\\20130216'  >>> s = r"f\o" >>> s            #repr representation 'f\\o' >>> len(s)   #length is 3, as there's only one `'\'` 3 

But when you're going to print this string you'll not get '\\' in the output.

>>> print strs C:\Users\Josh\Desktop\20130216 

If you want the string to show '\\' during print then use str.replace:

>>> new_strs = strs.replace('\\','\\\\') >>> print new_strs C:\\Users\\Josh\\Desktop\\20130216 

repr version will now show \\\\:

>>> new_strs 'C:\\\\Users\\\\Josh\\\\Desktop\\\\20130216' 
like image 114
Ashwini Chaudhary Avatar answered Sep 22 '22 23:09

Ashwini Chaudhary


Let me make it simple and clear. Lets use the re module in python to escape the special characters.

Python script :

import re s = "C:\Users\Josh\Desktop" print s print re.escape(s) 

Output :

C:\Users\Josh\Desktop C:\\Users\\Josh\\Desktop 

Explanation :

Now observe that re.escape function on escaping the special chars in the given string we able to add an other backslash before each backslash, and finally the output results in a double backslash, the desired output.

Hope this helps you.

like image 44
Rewanth Tammana Avatar answered Sep 19 '22 23:09

Rewanth Tammana