更新时间:2023-12-03 13:54:16
您可以使用 和
来完成.
You could do it using and
.
...
with open('example_file', 'w') as new_file:
for line in file_content:
currentLine = line.strip("\n")
if currentLine != 'example_line_1' and currentLine != 'example_line_2':
new_file.write(line)
new_file.close()
但这变得太大了,太快了.您还可以使用一个包含要从一行中删除的单词的数组,然后检查当前行是否包含这些单词中的任何一个:
but that gets too big, too fast. You could also use an array with words you wish to remove from a line and then just check if the current line consists of any of those words:
...
words = ["example_line_1", "example_line_2", "foobar"]
with open('example_file', 'w') as new_file:
for line in file_content:
currentLine = line.strip("\n")
if currentLine not in words:
new_file.write(line)
new_file.close()