You can use the string .replace() method along with rawstring.
Python 2:
>>> print r'pictures\12761_1.jpg'.replace("\\", "/")
pictures/12761_1.jpg
Python 3:
>>> print(r'pictures\12761_1.jpg'.replace("\\", "/"))
pictures/12761_1.jpg
There are two things to notice here:
- Firstly to read the text as a drawstring by putting r before the string. If you don't give that, there will be a Unicode error here.
- And also that there were two backslashes given inside the replace method's first argument. The reason for that is that backslash is a literal used with other letters to work as an escape sequence. Now you might wonder what is an escape sequence. So an escape sequence is a sequence of characters that doesn't represent itself when used inside string literal or character. It is composed of two or more characters starting with a backslash. Like '\n' represents a newline and similarly there are many. So to escape backslash itself which is usually an initiation of an escape sequence, we use another backslash to escape it.
I know the second part is bit confusing but I hope it made some sense.
Answer from user1114 on Stack OverflowYou can use the string .replace() method along with rawstring.
Python 2:
>>> print r'pictures\12761_1.jpg'.replace("\\", "/")
pictures/12761_1.jpg
Python 3:
>>> print(r'pictures\12761_1.jpg'.replace("\\", "/"))
pictures/12761_1.jpg
There are two things to notice here:
- Firstly to read the text as a drawstring by putting r before the string. If you don't give that, there will be a Unicode error here.
- And also that there were two backslashes given inside the replace method's first argument. The reason for that is that backslash is a literal used with other letters to work as an escape sequence. Now you might wonder what is an escape sequence. So an escape sequence is a sequence of characters that doesn't represent itself when used inside string literal or character. It is composed of two or more characters starting with a backslash. Like '\n' represents a newline and similarly there are many. So to escape backslash itself which is usually an initiation of an escape sequence, we use another backslash to escape it.
I know the second part is bit confusing but I hope it made some sense.
You can also use split/join:
print "/".join(r'pictures\12761_1.jpg'.split("\\"))
EDITED:
The other way you may use is to prepare data during it's retrieving(e.g. the idea is to update string before assign to variable) - for example:
f = open('c:\\tst.txt', "r")
print f.readline().replace('\\','/')
>>>'pictures/12761_1.jpg\n'
how to replace back slash character with empty string in python - Stack Overflow
how I can replace backslashes in python - Stack Overflow
remove backslash commands from string
Replace Backslashes with Forward Slashes in Python - Stack Overflow
The goal: 'apple' -> 'app\e'
This doesn't work:
>>> "apple".replace('l', '\\')
'app\\e'This also doesn't work:
>>> "apple".replace('l', '\')
File "<stdin>", line 1
"apple".replace('l', '\')
^
SyntaxError: EOL while scanning string literalWe need to specify that we want to replace a string that contains a single backslash. We cannot write that as "\", because the backslash is escaping the intended closing double-quote. We also cannot use a raw string literal for this: r"\" does not work.
Instead, we simply escape the backslash using another backslash:
result = string.replace("\\","")
The error is because you did not add a escape character to your '\', you should give \\ for backslash (\)
In [147]: foo = "a\c\d" # example string with backslashes
In [148]: foo
Out[148]: 'a\\c\\d'
In [149]: foo.replace('\\', " ")
Out[149]: 'a c d'
In [150]: foo.replace('\\', "")
Out[150]: 'acd'
So I have a string, and I want to remove all "words" in it, that have a backslash in them, like
'\t' '\t\t\t\t\t\t\t\t\t '
I can't use isalnum or similar, because the string also has chinese/russian characters, that i don't want to delete
Edit: thanks for the tips, I've got it now :)
Elaborating this answer, with pathlib you can use the as_posix method:
>>> import pathlib
>>> p = pathlib.PureWindowsPath(r'\dir\anotherdir\foodir\more')
>>> print(p)
\dir\anotherdir\foodir\more
>>> print(p.as_posix())
/dir/anotherdir/foodir/more
>>> str(p)
'\\dir\\anotherdir\\foodir\\more'
>>> str(p.as_posix())
'/dir/anotherdir/foodir/more'
Doesn't this work:
>>> s = 'a\\b'
>>> s
'a\\b'
>>> print s
a\b
>>> s.replace('\\','/')
'a/b'
?
EDIT:
Of course this is a string-based solution, and using os.path is wiser if you're dealing with filesystem paths.
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'
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.
Trying to create a script that prompts a user with a file path but want to replace all back slashes with forward slashes to avoid any escaped characters when doing things with that file.
I’m in python 3.10 if needed.
See Jamie Zawinksi's famous quote about regular expressions. Try to only resort to the use of re's when absolutely necessary. In this case, it isn't.
The actual content of string str (bad name for a variable, by the way, since there's a built-in type of that name) is
"abc INC\","None", "0", "test"
Why not just
str.replace('\\"', '|"')
which will do exactly what you want.
You can use the following positive lookahead assertion '\\(?=")':
import re
my_str = "\"abc INC\\\",\"None\", \"0\", \"test\""
p = re.sub(r'\\(?=")', '|', my_str)
print(p)
# '"abc INC|","None", "0", "test"'
Try not to use builtin names as names for variables, viz. str, to avoid shadowing the builtin.