在python文件中用“替换'\”'

吐鲁番

我正在尝试读取 line 然后用 '"' 替换 '"' this 。我的代码有什么问题吗。它正在执行但没有替换这两个字符:

# 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('\\"', '"')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章