0
我有腳本可以打開並讀取文本文件,分隔每個單詞並製作這些單詞的列表。我讓Counter來清單列出每個單詞出現的次數。然後我想在.csv文件中的每一行的東西出口是這樣的:將Python計數器導出爲.CSV文件
單詞Hello出現10次
字房子出現5次
字樹出現3次
...而等等
你能告訴我我需要改變什麼來使腳本工作嗎?
from collections import Counter
import re
import csv
cnt = Counter()
writefile = open('test1.csv', 'wb')
writer = csv.writer(writefile)
with open('screenplay.txt') as file: #Open .txt file with text
text = file.read().lower()
file.close()
text = re.sub('[^a-z\ \']+', " ", text)
words = list(text.split()) #Making list of each word
for word in words:
cnt[word] += 1 #Counting how many times word appear
for key, count in cnt.iteritems():
key = text
writer.writerow([cnt[word]])
謝謝!它工作,我沒有組織代碼,所以我忘了第二個循環來檢查。 – Ukii