作者sariel0322 (sariel)
看板Python
标题[问题] 大量资料程式抓取
时间Mon Dec 22 17:09:32 2014
大家好,我想在一个csv里面
抓取可能只出现的一笔的那一行资料(或两笔、三笔)
我写了一个code,希望能用最快的速度将资料抓出来
已经在server上跑了结果似乎是卡住了?
目前问题: 有试过比较小资料量的资料,跑出来是可以的
可能是我的资料量太大,因此他跑到出现我设定的"start output"就静止在那边了
以下是我的code:
import csv
from collections import defaultdict
protein_table = defaultdict(list)
P = []
a = int(raw_input("times: "))
out = str(a+1) + " domain protein.csv"
o = open(out,"w")
f = open("multiple domain protein.csv","r")
for row in csv.reader(f):
P.append(row[1])
protein_table[row[1]].append(row[0]+","+row[1]+","+row[2]+","+row[3]+","+row[4]+"\n")
print "----------------------start output-------------------"
for i in [k for k in P if P.count(k)==a]:
if i in protein_table:
for protein in protein_table[i]:
o.write(protein)
o.flush()
o.close()
f.close()
请问大家有什麽比较好修改的地方吗?
还是得写跑比较久的回圈之类的
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 120.126.36.171
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1419239375.A.EE5.html
1F:→ alibuda174: 把start output之後的程式码改成: 不知道可不可以 12/22 18:16
2F:→ alibuda174: for i in protein_table: 12/22 18:16
3F:→ alibuda174: if len(protein_table[i]) == a: 12/22 18:16
4F:→ alibuda174: for p in protein_table[i]: 12/22 18:16
5F:→ alibuda174: o.write(p) 12/22 18:16
6F:→ alibuda174: o.flush() 12/22 18:17
7F:→ alibuda174: 啊,抱歉...不太对。 12/22 18:18
8F:→ alibuda174: 请试试看吧...:D 12/22 18:18
9F:→ ccwang002: P.count 改成 Counter(P) 或 [k for k in set(P) ...] 12/22 18:25
10F:→ ccwang002: 需要实测一下 Ref: stackoverflow.com/a/12452678 12/22 18:26
11F:推 jimmytzeng: 何不直接将csv汇入sqlite,在透过sql语法去搜寻? 12/23 08:25
12F:推 polom: 可以用re 模组(正则) 抽象化处理 01/26 22:18