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    
wjandrea
  • 28,235
  • 9
  • 60
  • 81
mill
  • 360
  • 2
  • 3
  • 12
  • Does this other [question/answer](http://stackoverflow.com/a/10585406/1167750) help? *(double escaping?)* – summea May 21 '13 at 15:42
  • What does `string` look like? – melwil May 21 '13 at 15:43
  • string = 'class=\\"highlight' – mill May 21 '13 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 – mill May 21 '13 at 15:47
  • @summea no, that actually looks like a duplicate answer as the last one you posted – mill May 21 '13 at 15:58
  • @mill Actually I just edited the comment to show the answer first... but, it looks like [this issue](http://docs.python.org/2/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash) goes further than I previously thought. Inbar Rose has a nice alternative... although if you are working with path names in Windows (or something,) you may want to look into using [a different approach](http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/). – summea May 21 '13 at 16:10
  • The desired result `'class=\"highlight'` **doesn't make any sense**. There is no `\"` escape sequence. If you want the string to contain an actual backslash, then it will show `\\` when you display the *representation of* the string, but it only contains one backslash. – Karl Knechtel Aug 04 '22 at 20:29
  • Which is to say: the displayed output - `Out [6]: 'class=\\"highlight'` - **is** the result you want. This string has **one** backslash in it, which is being **represented** as two. Voting to close as not reproducible. – Karl Knechtel Aug 05 '22 at 23:17

4 Answers4

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 '

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • 9
    This works with the print, but not without it. print s.replace('\\\\', '\\') => some \ doubles . But s.replace('\\\\', '\\') => some \\ doubles – mill May 21 '13 at 15:54
  • 1
    `string.replace()` returns the object, you would have to to `s = s.replace()` – Inbar Rose May 21 '13 at 16:01
  • 5
    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 May 21 '13 at 16:11
  • @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...? – summea May 21 '13 at 16:22
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
mogul
  • 4,441
  • 1
  • 18
  • 22
2

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
Ruben
  • 187
  • 1
  • 7
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!

BuJay
  • 115
  • 1
  • 12