Re: [問題] 大量資料程式抓取
我的寫法,會讀取csv檔兩次,
而且輸出檔案並沒有把row[1]相同的放在一起。
import csv
table = {}
f = open("multiple domain protein.csv","r")
# 記錄row[1]出現的次數
for row in csv.reader(f):
try:
table[row[1]] += 1
except KeyError:
table[row[1]] = 1
f.close()
a = int(raw_input("times: "))
# 只留下需要的
table_a = {k: a for k in table if table[k] == a}
print("----------------------start output-------------------")
out = str(a+1) + " domain protein.csv"
o = open(out,"w")
f = open("multiple domain protein.csv","r")
# 再讀一次檔案,如果row[1]是想要的,輸出該行
for row in csv.reader(f):
if row[1] in table_a:
o.write(row[0]+","+row[1]+","+row[2]+","+row[3]+","+row[4]+"\n")
f.close()
o.flush()
o.close()
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.225.86.119
※ 文章網址: http://www.ptt.cc/bbs/Python/M.1419244841.A.6C5.html
※ 編輯: alibuda174 (36.225.86.119), 12/22/2014 20:12:25
推
12/22 21:31, , 1F
12/22 21:31, 1F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
2
12
Python 近期熱門文章
PTT數位生活區 即時熱門文章