我有一个文本文件,其中包含 ' 和 " 格式的引号。该文本文件的格式为。
"string1", "string2", 'string3', 'string4', ''string5'', etc.
如何删除所有引号 ' 和 " 而同时保留文件的其余部分现在的样子?
我猜应该是这样的:
with open('input.txt', 'r') as f, open('output.txt', 'w') as fo:
for line in f:
fo.write(line.strip())
line.strip() 以某种方式去除引号字符串。还需要什么吗?
请您参考如下方法:
你很接近。代替 str.strip(),试试 str.replace():
with open('input.txt', 'r') as f, open('output.txt', 'w') as fo:
for line in f:
fo.write(line.replace('"', '').replace("'", ""))
