You need to escape the backslash before the "3"
Even if the backslash is in the quotes it will NOT be escaped.
If you're programitically inserting the value in the function, then I suggest performing another .replace and replace the "\" with a "-"
That should convert the string to "21-3-90" and if you need to break it down further, you can replace the "-" to a " " as you originally intended.
akhter@uf8b156e44b21553641ed:~/PycharmProjects/untitled2$ python3.2
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def process(date):
... date = date.replace("\\", " ")
... print(date)
...
>>> process("21\\3\\90")
21 3 90
>>> process("13\\4\\90")
13 4 90
>>>
akhter@uf8b156e44b21553641ed:~/PycharmProjects/untitled2$ python2.7
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def process(date):
... date = date.replace("\\", " ")
... print(date)
...
>>> process("21\\3\\90")
21 3 90
>>>
akhter@uf8b156e44b21553641ed:~/PycharmProjects/untitled2$ python3.2
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def process(date):
... date = date.replace('\\', ' ')
... print(date)
...
>>> process("21\\3\\90")
21 3 90
>>>
Answer from Mo Ali on Stack Overflowhow to replace back slash character with empty string in python - Stack Overflow
python - Replacing everything with a backslash till next white space - Stack Overflow
String replace with backslashes in Python - Stack Overflow
string - python replace single backslash with double backslash - 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'
Assuming a 'tag' can also occur at the very beginning of your string, and avoid selecting false positives, maybe you could use:
\s?(?<!\S)\\[a-z\d]+
And replace with nothing. See an online demo.
\s?- Optionally match a whitespace character (if a tag is mid-string and therefor preceded by a space);(?<!\S)- Assert position is not preceded by a non-whitespace character (to allow a position at the start of your input);\\- A literal backslash.[a-z\d]+- 1+ (Greedy) Characters as per given class.
First, the / doesn't belong in the regular expression at all.
Second, even though you are using a raw string literal, \ itself has special meaning to the regular expression engine, so you still need to escape it. (Without a raw string literal, you would need '\\\\fs\\d+'.) The \ before f is meant to be used literally; the \ before d is part of the character class matching the digits.
Finally, sub takes three arguments: the pattern, the replacement text, and the string on which to perform the replacement.
>>> re.sub(r'\\fs\d+', '', r"This is a string \fs24 and it contains...")
'This is a string and it contains...'
You're getting what you want. It just doesn't look that way in the REPL:
>>> 'asdf hjkl'.replace(' ', '\s')[4]
'\\'
As you can see, that's one character, not two.
Try printing it:
>>> print 'asdf hjkl'.replace(' ', '\s')
asdf\shjkl
The result is only displayed, try the following,
a = 'asdf hjkl'.replace(' ','\s')
print a
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.
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.
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.
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'