看板 Python 關於我們 聯絡資訊
我的寫法,會讀取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
sariel0322: 測試過後可以,非常感謝 12/22 21:31