【发布时间】:2021-03-21 09:57:06
【问题描述】:
我想从 .txt 文件中删除行。 我想为要删除的字符串列出一个列表,但代码会多次粘贴这些行 列表中的尽可能多的字符串。如何避免?
file1 = open("base.txt", encoding="utf-8", errors="ignore")
Lines = file1.readlines()
file1.close()
not_needed = ['asd', '123', 'xyz']
row = 0
result = open("result.txt", "w", encoding="utf-8")
for line in Lines:
for item in not_needed:
if item not in line:
row += 1
result.write(str(row) + ": " + line)
所以如果该行包含列表中的字符串,则将其删除。 在每个字符串之后打印不带行的文件。 怎么做?
【问题讨论】: