python replace single backslash with double backslash
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'
python3 replacing double backslash with single backslash
Take a closer look at the string, they are all single slash.
In [26]: my_str[0]
Out[26]: '\\'
In [27]: my_str[1]
Out[27]: 'x'
In [28]: len(my_str[0])
Out[28]: 1
And my_str.replace('\\','\')
won't work because the token here is \'
, which escapes '
and waits for the another closing '
.
Use my_str.replace('\\', '')
instead
Update: after few more days, I realize the following discussion may also be helpful. If the intension of a string with escape ('\\x'
or '\\u'
) are eventually hex/unicode literals, they can be decoded by escape_decode
.
import codecs
print(len(b'\x32'), b'\x32') # 1 hex literal, '\x32' == '2'
print(len(b'\\x32'), b'\\x32') # 4 chars including escapes
print(codecs.escape_decode('\\x32', 'hex')) # chars->literal, 4->1
# 1 b'2'
# 4 b'\\x32'
# (b'2', 4)
s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'
ed, _ = codecs.escape_decode(s, 'hex')
print(len(s), s)
print(len(ed), ed)
# 49 \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z
# 22 b'\xa5\xc0\xe6aK\xf9\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'
How to replace double backslash to single backslash
a = 'RXIE-SERVER\\MSSQLSERVER_NEW'
doesn't have a double backslash. It has an escaped single backslash, it's just safer (and will eventually be required) to escape it so Python doesn't think \M
is an attempt at a string escape. If you do:
print(a)
you'll see it only prints one backslash (because print
outputs the raw data without showing escapes).
The reason a.replace('\\', '')
doesn't work is because it replaced the single backslash with nothing (and it would do so for all backslashes); a.replace('\\\\', '\\')
doesn't work because '\\\\'
represents the actual doubled backslash, and you don't have any of those.
If your input came from some other source (not the literal you described) and actually has a doubled-backslash, then a.replace('\\\\', '\\')
actually worked, but REPL's echo the repr
of the object, and for str
, that means adding the backslash escape to make it a legal, equivalent str
literal, so it looked like a double-backslash, but only had one. If you change >>> a.replace('\\\\', '\\')
to >>> print(a.replace('\\\\', '\\'))
(which prints the human-friendly form, not the repr
), you'll see it display only a single backslash.
If you don't like how it looks in your code, use raw strings to remove the need for the escape:
a = r'RXIE-SERVER\MSSQLSERVER_NEW'
# ^ note prefix that makes it raw
Python regex to replace double backslash with single backslash
why not use string.replace()
?
>>> s = 'some \\\\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Or with "raw" strings:
>>> s = r'some \\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Since the escape character is complicated, you still need to escape it so it does not escape the '
Related Topics
Combine Lists with Common Elements
Find Column Name in Pandas That Matches an Array
Numpy Sum Elements in Array Based on Its Value
How to Use Qscrollarea to Make Scrollbars Appear
Filling in Login Forms in Instagram Using Selenium and Webdriver (Chrome) Python Osx
Separation of Business Logic and Data Access in Django
How to Remove Packages Installed with Python's Easy_Install
Peak-Finding Algorithm for Python/Scipy
Convert a List of Characters into a String
Is a Python Dictionary an Example of a Hash Table
Execute Terminal Command from Python in New Terminal Window