我想搜索txt文件中的列表元素

2024-09-28 19:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在txt文件中搜索列表元素。然而,使用这段代码,我没有得到期望的结果。我试过几种解决办法,但都不管用

def run(self):
    hostname = self.queue.get()
    with open('allhosts.txt', "r+") as file1:
        fileline1 = file1.readlines()
        for x in hostname:
            for line in fileline1:
                if x in line:
                    self.found.emit(hostname)
                else:
                    self.notFound.emit(hostname)

Tags: 文件代码inselftxt元素列表for