2

我正在尝试在大型文本文件中搜索单词列表。我认为列表会更容易,而不是为每个单词一遍又一遍地运行命令,但我不确定如何去做。下面的脚本或多或少适用于字符串值,但我想用“dict”列表的每个值替换下面的“字符串”。

import csv

count = 0
dic = open('dictionary','r') #changed from "dict" in original post
reader = csv.reader(dic)
allRows = [row for row in reader]
with open('bigfile.log','r') in inF:
   for line in inF:
      if 'string' in line: #<---replace the 'string' with dict values
         count += 1
count
4

1 回答 1

4

将您的文件转换为一个集合:

 with open('dictionary','r') as d:
     sites = set(l.strip() for l in d)

现在,您可以对每行进行有效的成员资格测试,前提是您可以拆分行

with open('bigfile.log','r') as inF:
   for line in inF:
       elements = line.split()
       if sites.intersection(elements):
           count += 1
于 2013-06-30T13:31:23.263 回答