我已經堆疊在我的Python腳本中,嘗試打開.txt文件,從該文件中創建單詞列表,計算單詞出現次數(Counter)並將其放入在.csv文件中。我的文件從1870.txt - 1892(1871,1872,1873..1892.txt)獲得了名稱。一切從那裏工作,但我希望每個文件放在下一列。Python在.CSV文件中創建新列和存儲數據
def putInExcel(outputt):
i = 1790
while i < 1892:
inputt = str(i) + '.txt' #Making text file name
writefile = open(outputt)
writer = csv.writer(writefile)
with open(inputt) as file: #Separating each word and storing in list
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()
i = i+1
此腳本正在工作,但它將所有內容存儲在一列中。 有沒有人有一些想法?謝謝!
這正是我所期待的!但我正試圖理解並連接代碼 – Ukii
我不明白'...'對於... 1791的含義... ...。對不起,但我很難從一個代碼移動到另一個:) – Ukii
這意味着「無論你需要放在這裏」。我試圖弄清楚它在什麼環路中。 –