为什么还在打印

Why is it still printing

我有 2 个 txt 文件,其中一个有多余的行。我只想提取额外的行。它不断打印所有内容。为什么?我是说如果 txt1 的第一行不等于 txt 2 的第一行然后打印它。

import os, sys

htmlRub = ""

path = "./filter.txt"
if os.path.isfile(path):
    oFile = open(path)
    filter = oFile.read()
    oFile.close()
else:
    print("Filter file is missing")

path = "./database.txt" #The HTML code downloaded
if os.path.isfile(path):
    oFile = open(path)
    htmlRub = oFile.read() #The HTML code downloaded
    oFile.close()
else:
    print("Database file is missing")

filterData = filter.split("\n")
htmlData = htmlRub.split("\n") #The HTML code downloaded


for line in htmlData:
    for lineagain in filterData:
        if line != lineagain:
            print(line)
            break
        else:
            pass
            break

如果我的理解正确,您是想从 htmlData 中删除 filterData 中的所有字符串。我希望。

delta = [s for s in htmlData if s not in filterData]
for s in delta:
    print s

如评论所述,您的循环没有按照您的想法进行。 列表理解的一种循环格式是:

for hline in htmlData:
    if hline not in filterData:
        print hline

克里斯托弗,

为了让您的代码基本保持不变,看起来您只需要更改:

if line != lineagain:
    print(line)
else:
    pass
    break

至:

if line != lineagain:
    print(line)
else:
    continue

robert_x44的回答有点"Pythonic",好像是