Python:如何从文本文件中删除所有引用?

2024-09-25 10:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件,它有'"形式的引用。此文本文件的格式为。

"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()以某种方式除去引号的字符串。还需要什么吗?


Tags: txtas格式lineopen形式引号strip
1条回答
网友
1楼 · 发布于 2024-09-25 10:29:36

你很接近。与其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("'", ""))

相关问题 更多 >