我有一个这样的字符串:
str1 = "/path/happy (dog)"
出于某种目的,我想要它:
str2 = "/path/happy\ \(dog\)"
我尝试使用重新:
str1 = "happy (dog)"
tuple_1 = ('\s+','(',')')
for i in tuple_1:
match = re.search(r"("+i+")",str1)
if match:
str1 = re.sub(match.group(),"\\"+match.group(),str1)
print str1
但它给出了错误:
sre_constants.error: unbalanced parenthesis
我知道我在这里遗漏了一些东西......