【问题标题】:replace '\"' with " in python files在 python 文件中用 " 替换 '\"'
【发布时间】:2021-10-19 21:40:19
【问题描述】:

我正在尝试读取行,然后将 '"' 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()

【问题讨论】:

    标签: python-3.x csv replace


    【解决方案1】:

    我不确定我是否完全理解您的要求。我的理解是你想用"替换\"

    在这种情况下,您会错过另一个反斜杠,因为反斜杠本身是一个特殊字符,您需要使用 \ 转义它

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

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 2020-12-12
      • 1970-01-01
      • 2011-06-12
      • 2015-10-03
      • 2019-07-30
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多