2016-05-12 83 views
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]]) 

回答

1

最大的問題是你的第二個for循環發生在每個單詞的每一個出現處,而不是每個獨特的單詞一次。您需要清除整個循環,以便在完成計數後執行。嘗試這樣的:

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: 
    text = file.read().lower() 
    text = re.sub('[^a-z\ \']+', " ", text) 
    words = list(text.split()) 
    for word in words: 
     cnt[word] += 1 
    for key, count in cnt.iteritems(): #De-dent this block 
     writer.writerow([key,count]) #Output both the key and the count 

writefile.close() #Make sure to close your file to guarantee it gets flushed 
+0

謝謝!它工作,我沒有組織代碼,所以我忘了第二個循環來檢查。 – Ukii