簡體   English   中英

如何拆分此計數器並將其導出到.csv?

[英]How can I split this counter and export it to .csv?

我正在研究此Python(2.7)代碼,正在分析.txt文件中最常見的50個單詞。 下一步是將單詞及其出現次數導出到.csv文件中。 我正在導出到.csv文件,但是代碼始終將單詞及其編號與標點符號和括號組合在一起。 我需要兩列,每組一個新行。

例如:('the',329)需要顯示為兩個不同的列, 329

我想我可以使用正則表達式來實現它,但是我真的不知道該怎么做。 任何幫助表示贊賞。

import re
import collections
import csv
from collections import Counter

words = re.findall('\w+', open('document.txt').read().lower())
thing = Counter(words).most_common(50)

PDFiles = "PDFiles.csv"
with open(PDFiles, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in thing:
            writer.writerow(val) # edited

使用最新的編輯,文本顯示為

tell     | 329
        0| 65

嘗試這個

writer.writerow([val [0],val [1]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM