【发布时间】:2020-03-24 07:51:48
【问题描述】:
我写了一个代码,计算所有文本单词和出现次数,输出为:
doctype 1
html 3
dir 1
rtl 5
lang 1
head 17
我想将此输出以 word 和 count 逗号分隔的格式写入 csv 文件。
# Open the file in read mode
text = open("output.txt", "r")
# Create an empty dictionary
d = dict()
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = line.strip()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = line.lower()
# Split the line into words
words = line.split(" ")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
# Print the contents of dictionary
for key in list(d.keys()):
print ( key, ":", d[key])
【问题讨论】:
-
另外,你可以试试
collections.Counter