我被困在这里了。假设我有一个如下所示的文本文件 (example.txt):
Generic line 1() 46536.buildsomething
Generic line 2() 98452.constructsomething
Something I'm interested in seeing
Another common line() blablabla abc945
Yet another common line() runningoutofideashere.923954
Another line I'm interested in seeing
Line I don't care about 1() yaddayaddayadda
Line I don't care about 2() yaddayaddayadda
Generic line 3() 23485.buildsomething
Yet some other common line
我现在有一个排除文本文件 (exclusions.txt),其中包含不打印的部分行:
Generic
common
don't care about
我的想法是我想打开 example.txt 文件,打开 excludes.txt 文件,然后打印 example.txt 中不包含 excludes.txt 中任何行的任何行。
到目前为止我尝试过的(没有任何成功):
textfile = open("example.txt", "r")
textfile = textfile.readlines()
exclusionslist = []
exclusions = open("exclusions.txt", "r")
exclusions = exclusions.readlines()
for line in exclusions:
exclusionslist.append(line.rstrip('\n'))
for excline in exclusions:
for line in textfile:
if exline not in line:
print line
我想我知道问题是什么,但我不知道如何解决它。我想我只需要告诉 Python 如果文本文件中的一行包含排除项中的任何行,请不要打印它。