如何确定列表中的任何元素是否在.txt文件中找到?

问题描述 投票:-4回答:2

我已经通过Tweety导入了我的商业对手的时间表。我想搜索从文件导入的一些关键字。

f = open('base.txt','r')
with open('base.txt') as f:
    content = [f.read()]

我想找到他是否使用了我的关键字列表中的任何字词。你能帮助我吗?

python arrays tweepy
2个回答
0
投票

据我所知,你的问题信息很少,mmap库可以解决你的问题

import mmap

with open('test.txt', 'rb', 0) as file, mmap.mmap(file.fileno(), 0, 
access=mmap.ACCESS_READ) as s:
# test.txt has a sentence "where am i? I am here!"
    keywords = ["ie","ame!","here"]
    for idx,keyword in enumerate(keywords):
        if s.find(bytes(keyword,encoding ="utf8")) != -1:
            print('True')
            print("Keyword is" ,keyword)
        else:
            print('Nothing')
# Nothing
# Nothing
# True
# Keyword is here

0
投票

循环显示单词列表,检查每个单词是否出现在另一个列表中。

for word in list:
    if word in other_list:
        print(word)
© www.soinside.com 2019 - 2024. All rights reserved.