2013-07-15 73 views
0

我是python新手,目前正在尋找一種對多個CSV文件中的關鍵字進行計數的方法。該文件具有一般結構如下:對多個CSV文件中的條目進行計數

PDB ID NDB ID Structure Title Citation Title  Abstract 
1ARJ 1ARJ ARG-bound TAR Structure of  Solution structure of Arginine 
       RNA    some complex RNA RNA complex. 

我想獲得關於如何寫一個導入多個CSV文件或循環這些文件的輸入代碼的一些指導,然後返回多少倍的值關鍵字出現在記錄中,哪個字段包含命中。我已經看到了一些csv導入模塊的教程,但我不知道如何使用它。

import csv 
my_list = mRNA 


words = {} 
for items in my_list: 
    for item in items.split(', '): 
     words.setdefault(item, 0) 
     words[item] += 1 

with open('mrna.csv', 'w') as fopen: 
    writer = csv.writer(fopen) 
    for word, count in words.items(): 
     writer.writerow([word, count]) 
+0

我查閱了關於使用csv導入模塊的教程並撰寫了這篇文章。導入CSV my_list = mRNA的 字= {} 用於my_list項目:在items.split 對於項目( ''): words.setdefault(項目,0) 詞語[項目] + = 1 with open('mrna.csv','w')as fopen: writer = csv.writer(fopen) for word,count in words.items(): writer.writerow([word,count]) –

回答

0

mRNA哪裏定義?也許你應該重新閱讀csv module examples並嘗試從頭開始,在嘗試計算關鍵字之前嘗試輸出CSV文件中的數據。

相關問題