我有大约 20000 个文本文件,编号为 1.txt、2.txt 等等。
现在,我正在创建一个字典 d,其中包含文件 5.txt、10.txt、15.txt 等的文件路径。
d[value]=filepath
ex:
d[5]=d:/articles/5.txt
d[45]=d:/articles/45.txt
我有一个包含 500 个单词的文本文件“temp.txt”
vs
mln
money
等等..
现在对于字典“d”中的每个文本文件,我需要记录列表中所有单词的出现频率。
所以,我创建了一个 d2[word][file]=count 形式的嵌套字典(这是正确的方法吗?)
where, d2[vs][5]=number of times "vs" occurs in 5.txt
简而言之,对于每个文件,我都会遍历单词列表并计算其出现次数。
我如何创建 d2?
我的错误代码是:
import collections, sys, os, re
sys.stdout=open('3.txt','w')
from collections import Counter
from glob import glob
folderpath='d:/individual-articles'
folderpaths='d:/individual-articles/'
counter=Counter()
filepaths = glob(os.path.join(folderpath,'*.txt'))
# returns the next word in the file
def words_generator(fileobj):
for line in fileobj:
for word in line.split():
yield word
d= collections.defaultdict(list)
#to print the filenames:(creation of d)
with open('topics.txt','r') as f:
for line in f.readlines():
value=(line.split('~')[0])
if int(value)%5==0:
file=folderpaths+value+'.txt'
d[value].append(file)
d2= collections.defaultdict(list)
for file in filepaths:
f = open(file,"r")
words = words_generator(f)
for word in words:
if file in d[file]:
d2[word][file]+= 1
#i have no idea how to go further, beyond this point.
请帮忙!!