替换 python 文件中的 '\ "' with "



我正在尝试阅读一行,然后用'"'替换'"'这个。我的代码是否有任何问题。它正在执行,但没有替换这两个字符:

# Read in the file
with open('filename', 'r') as file :
filedata = file.read()
filedata = filedata.replace('"', ' "')
with open('filename', 'w') as file:
file.write(filedata)
file.close()

我不确定我是否完全理解你的问题。我的理解是你想用"代替"

在这种情况下,您将错过另一个反斜杠本身是一个特殊字符,您需要使用

转义
with open('filename', 'r') as file:
filedata = file.read()
filedata = filedata.replace('\"', '"')

最新更新