我已经完成了所有主题,但仍然无法解决问题
def process(date)
#here how to know that I need to convert date into raw literal
date = date.replace('\\', ' ')
process("21\3\90")
输出
21 90
3被跳过
虽然在这里工作
print r'pictures\12\761_1.jpg'.replace("\\", " ")
输出
pictures 12 761_1.jpg
将21\12\1234
转换为21 12 1234
答案 0 :(得分:1)
你需要在" 3"之前逃避反斜杠。 即使反斜杠在引号中,它也不会被转义 如果您在函数中以编程方式插入值,那么我建议您执行另一个.replace并替换" \"使用" - "
那应该将字符串转换为" 21-3-90"如果你需要进一步细分,你可以替换" - "到了#34; "正如你原先的意图。
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
>>>