我希望文本是一个键:计数行。 现在它把文件保存成一个普通字典,我搞不懂。在
def makeFile(textSorted, newFile) :
dictionary = {}
counts = {}
for word in textSorted :
dictionary[word] = counts
counts[word] = counts.get(word, 0) + 1
# Save it to a file
with open(newFile, "w") as file :
file.write(str(counts))
file.close()
return counts
//非常基本的字典到文件打印机
您可以这样做,这是几行,其中有一个CounterDict和csv模块:
如果只想存储键/值对,那么使用两个字典是没有意义的。一个单独的计数器dict将得到所有单词和csv.writerows将每一对用冒号隔开,每行一对。在
试试这个
编辑:由于iteritems是从python3中删除的,所以将代码改为items()
相关问题 更多 >
编程相关推荐