我有一个文本文件,我想用\
替换字符,
。在this SO post中阅读@Jack Aidley的答案后:
# Read in the file
with open('file.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('n', '***IT WORKED!!!***')
# Write the file out again
with open('file.txt', 'w') as file:
file.write(filedata)
我可以成功地将诸如n
之类的简单字母的内容更改为***IT WORKED!!!***
。但是,如果我替换
filedata.replace('n', '***IT WORKED!!!***')
作者
filedata.replace('\', '***IT WORKED!!!***')
我收到语法错误:
SyntaxError: EOL while scanning string literal
如何用\
替换所有,
?