作者alibuda174 (阿哩不达)
看板Python
标题Re: [问题] 大量资料程式抓取
时间Mon Dec 22 18:40:38 2014
我的写法,会读取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://webptt.com/cn.aspx?n=bbs/Python/M.1419244841.A.6C5.html
※ 编辑: alibuda174 (36.225.86.119), 12/22/2014 20:12:25
1F:推 sariel0322: 测试过後可以,非常感谢 12/22 21:31