为什么还在打印

时间:2015-02-17 01:08:14

标签: python string for-loop split extract

我有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

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你试图从filterData中的htmlData中删除所有字符串。我希望。

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

答案 1 :(得分:1)

克里斯托弗,

为了保持代码大致相同,看起来您只需要更改:

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

为:

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

robert_x44的答案有点多了#34; Pythonic",就像它一样。