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

时间:2018-01-02 19:38:24

标签: python arrays tweepy

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

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

for word in list:
    if word in other_list:
        print(word)