Python 在文件中搜索术语
我的脚本有问题。我想在搜索列表中定义的不同文件中查找一些术语。但是,它不会找到这些术语,即使它们确实包含在这些文件中。我查过了 我错过什么了吗 路径='/mypath' 对于glob.globos.path.joinpath,'*.txt'中的文件名: 使用openfilename作为infp: mylist=infp.read.splitlines 对于mylist中的单词: 搜索列表=[term1、term2、term3] 对于搜索列表中的i: 如果我说: 打印“找到” 其他: 打印“未找到” 这可能会有帮助Python 在文件中搜索术语,python,file,search,Python,File,Search,我的脚本有问题。我想在搜索列表中定义的不同文件中查找一些术语。但是,它不会找到这些术语,即使它们确实包含在这些文件中。我查过了 我错过什么了吗 路径='/mypath' 对于glob.globos.path.joinpath,'*.txt'中的文件名: 使用openfilename作为infp: mylist=infp.read.splitlines 对于mylist中的单词: 搜索列表=[term1、term2、term3] 对于搜索列表中的i: 如果我说: 打印“找到” 其他: 打印“未找到
path = '/mypath'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(filename) as infp:
data = infp.read() #Just Read the content
searchlist = ["term1", "term2", "term3"]
for i in searchlist:
if i in data: #Check if search term in content.
print('found')
else:
print('not found')
我的列表是你的台词列表,而不是单词列表。您需要拆分行以获取单词,然后应用搜索。看,看看这个