13

I'm trying to replace all double backslashes with just a single backslash. I want to replace 'class=\\"highlight' with 'class=\"highlight'. I thought that python treats '\\' as one backslash and r'\\+' as a string with two backslashes. But when I try

In [5]: re.sub(r'\\+', '\\', string)
sre_constants.error: bogus escape (end of line)

So I tried switching the replace string with a raw string:

In [6]: re.sub(r'\\+', r'\\', string)
Out [6]: 'class=\\"highlight'

Which isn't what I need. So I tried only one backslash in the raw string:

In [7]: re.sub(r'\\+', r'\', string)
SyntaxError: EOL while scanning string literal    
8
  • Does this other question/answer help? (double escaping?) Commented May 21, 2013 at 15:42
  • What does string look like? Commented May 21, 2013 at 15:43
  • string = 'class=\\"highlight' Commented May 21, 2013 at 15:45
  • @summea sorry this doesn't help: re.sub('\\\\', '\\', string) just gives me 'adfd\\"adfadf' and re.sub('\\\\', '\', string) gives me an EOL SyntaxError Commented May 21, 2013 at 15:47
  • @summea no, that actually looks like a duplicate answer as the last one you posted Commented May 21, 2013 at 15:58

4 Answers 4

17

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 '

Sign up to request clarification or add additional context in comments.

6 Comments

This works with the print, but not without it. print s.replace('\\\\', '\\') => some \ doubles . But s.replace('\\\\', '\\') => some \\ doubles
string.replace() returns the object, you would have to to s = s.replace()
Sorry, this doesn't work. original_string = 'class=\\"highlight'; new_string = original_string.replace('\\\\', '\\'); new_string => 'class=\\"highlight'. The print statement removes the double backslash, not the replace. In fact, just doing print original_string => 'class=\"highlight', as well as print new_string => 'class=\"highlight'. You can also confirm this with new_string == original_string => True
@mill Wouldn't you still need the backslash to be escaped in the actual string/variable? :) Otherwise... how would it know you wanted a backslash left in the final string/variable result...?
How did this get a checkmark if it doesn't work. @mill is correct in saying it only works with the print. Still can't find simple solution to this other than maybe analyzing each byte programmatically.
|
3

Just use .replace() twice!

I had the following path: C:\\Users\\XXXXX\\Desktop\\PMI APP GIT\\pmi-app\\niton x5 test data

To convert \ to single backslashes, i just did the following:

path_to_file = path_to_file.replace('\\','*')
path_to_file = path_to_file.replace('**', '\\')

first operation creates ** for every \ and second operation escapes the first slash, replacing ** with a single \.

Result:

C:**Users**z0044wmy**Desktop**PMI APP GIT**pmi-app**GENERATED_REPORTS
C:\Users\z0044wmy\Desktop\PMI APP GIT\pmi-app\GENERATED_REPORTS

Comments

2

You only got one backslash in string:

>>> string = 'class=\\"highlight' 
>>> print string
class=\"highlight

Now lets put another one in there

>>> string = 'class=\\\\"highlight' 
>>> print string
class=\\"highlight

and then remove it again

>>> print re.sub('\\\\\\\\', r'\\', string)
class=\"highlight

Comments

0

I just realized that this might be the simplest answer:

import os
os.getcwd()

The above outputs a path with \ (2 back slashes)

BUT if you wrap it with a print function, i.e., print(os.getcwd()) it will output the 2 slashes with 1 slash so you can then copy and paste into an address bar!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.