我不知道为什么我找不到它,但我想用python替换特殊字符'\'
。在Python中替换''字符
我有一个字符串内我有'\'
字符,但我有信心找到解决方案,用' - '代替它。 这就是发生的事情,而我试图取代,
>>> x = 'hello\world'
>>> x
'hello\\world'
>>> x.replace('\', '-')
File "<stdin>", line 1
x.replace('\', '-')
SyntaxError: EOL while scanning string literal
编辑: 会尝试这一点,在Eclipse IDLE
x = 'hello\world'
print x
x.replace('\\', '-')
print x
输出:
hello\world
hello\world
退出反斜杠:''你好\ \世界'.replace('\\',' - ')' – falsetru
请检查编辑! – OpenCurious
@Heliconia:'replace'不改变字符串,它会返回一个新字符串。你需要说的是'x = x.replace('\',' - ')'。 – RichieHindle