用反斜杠替换双反斜杠

时间:2018-10-12 14:19:12

标签: python

我有这个字符串: feed_\\d{6}.txt.gz 并且我需要用单个反斜杠替换双反斜杠。我查看了其他答案(包括How to replace a double backslash with a single backslash in python?),但没有帮助。

有些东西不起作用

mystr = 'feed_\\d{6}.txt.gz'
mystr.replace('\\', '\')
# invalid syntax. string is unterminated.

mystr.replace('\\\\', '\\')
#returns the original string

import codecs
codecs.escape_decode(mystr)
# returns a tuple ('feed_\\d{6}.txt.gz', 17)
# Got this from the aforementioned other thread but it doesn't 
# actually explain how to use it

我很困惑。有什么想法吗?

0 个答案:

没有答案