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
24.6k8 gold badges53 silver badges73 bronze badges
asked May 21, 2013 at 15:39
8
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 '
answered May 21, 2013 at 15:42
Inbar RoseInbar Rose
39.8k24 gold badges82 silver badges128 bronze badges
4
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
answered May 21, 2013 at 15:50
mogulmogul
4,3361 gold badge17 silver badges22 bronze badges
0
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
answered Aug 3, 2021 at 8:16
RubenRuben
1571 silver badge6 bronze badges
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!
answered May 11, 2021 at 13:24
BuJayBuJay
11511 bronze badges
0
So... you'll need the app to do that
devRant on iOS & Android lets you do all the things like ++ or -- rants, post your own rants and comment on others' rants. You can also set up your profile. Get it now!
Free Swag!
Get a free stress ball if a rant you post gets 750 ++'s
Learn More
*Some restrictions apply, click "Learn More"
Verify Your Email
In order to vote, comment or post rants, you need to confirm your email address. You should have received a welcome email with a confirm link when you signed up. If you can't find the email, click the button below.
Resend Email
Rant
Your rant must be between 6 and 5,000 characters
Edit Rant
Your rant must be between 6 and 5,000 characters
Join devRant
Vote and comment on others' rants. Post your own. Build your custom avatar.
!
Must be a valid email address
!
Must be over 6 characters
Keep me logged in
By clicking "Sign Up", you agree to the Terms of Service & Privacy Policy. FYI we never show your email to other members.
Already on devRant?
LoginLogin
You know the deal
!
Email address already registered
!
Email and password do not match
Keep me logged in
Forgot?
It happens to the best of us. If you still need help, email
!
No account with that email address