Re: [問題] 大量資料程式抓取

看板Python作者 (阿哩不達)時間11年前 (2014/12/22 18:40), 11年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
我的寫法,會讀取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
文章代碼(AID): #1Kb_KfR5 (Python)
文章代碼(AID): #1Kb_KfR5 (Python)