python replace backslashes to slashes
Asked Answered
P

3

22

How can I escape the backslashes in the string: 'pictures\12761_1.jpg'?

I know about raw string. How can I convert str to raw if I take 'pictures\12761_1.jpg' value from xml file for example?

Permittivity answered 8/6, 2011 at 7:46 Comment(0)
O
34

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.

Oeuvre answered 8/6, 2011 at 7:48 Comment(0)
B
0

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'
Bavaria answered 8/6, 2011 at 7:51 Comment(6)
Did you read my question? I wrote that I know about r'pictures\12761_1.jpg'.split("\\") But how can I escape backslash if I already have variable with 'pictures\12761_1.jpg' value?Permittivity
Sorry if i missed this - but where you wrote this?Bavaria
I have tried the same without raw - e.g. typed in Python2.6 console the following: print "/".join('pictures\12761_1.jpg'.split("\\")) and it still works the same way. Or i miss something?Bavaria
If you try it in Python2.6 console you will see 'picturesW61_1.jpg'Permittivity
@Artsiom Rudzenka "I know about raw string" - sorry if thaw was not unclear.Permittivity
Sure, you right - it is my fault, i simply use another test string. The only way i see for now is to work with xml file or any other data source you are using. I made a small test - created a file f with 'pictures\12760_1.jpg' row inside it and then simply open this file, and call f.readline().replace('\\','/') or "/".join(f.readline().split("\\")).Bavaria
A
0

I know it is not what you asked exactly, but I think this will work better. Tit's better to just have the names of your directories and use os.path.join(directory,filename)

"os.path.join(path, *paths) Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component"

https://docs.python.org/2/library/os.path.html

Alexei answered 16/1, 2020 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.