Re: [問題] 如何在dbf檔中進行資料比對?已刪文

看板Python作者 (Neisseria)時間11年前 (2014/06/10 23:56), 11年前編輯推噓1(103)
留言4則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《ryoma0915 (芸~)》之銘言: : 我有一個這樣的dbf檔 : http://ppt.cc/nupm : AREA X Y id : 201144.20807 203500.00000 2535500.00000 2 : 410470.48979 204500.00000 2535500.00000 2 : 96374.47460 199500.00000 2533500.00000 2 : 118790.12873 196500.00000 2531500.00000 2 : 157389.24175 195500.00000 2530500.00000 2 : 514871.81518 196500.00000 2530500.00000 2 : 259449.53306 195500.00000 2529500.00000 2 : 602155.49015 196500.00000 2529500.00000 2 : 我想要找出 x y 相同的座標點 : 然後area 要進行相加 : 這是我的code : http://pastebin.com/4uwLj83F : 想問大家哪裡錯誤呢? : 謝謝大家^^ 講一下這段... i=0 for rec in range(record['X']): for re in range(record['Y']): if rec[i] == rec[i+1] & re[i] == re[i+1]: print 妳可能對 range 的用法會錯意 請在 interactive python 下看 help(range) 的說明... 例如:range(4) 傳回 [0, 1, 2, 3] 如果要走訪資料 要以讀入的 Dbf 物件跑迴圈才讀得到 DBF 檔的資料 這裡考慮簡單的情形,記憶體夠大可以載入所有的數據 from collections import defaultdict from dbfpy import dbf location = dict() db = dbf.Dbf("data.dbf") # 走訪 DBF 檔案,將數據進行加總 for rec in db: if not str(rec['X']) in location: location[str(rec['X'])] = defaultdict(float) location[str(rec['X'])][str(rec['Y'])] += float(rec['AREA']) db.close() for k_x in location.keys(): for k_y in location[k_x].keys(): print k_x, k_y, location[k_x][k_y] 參考看看 不過,如果這是作業或工作的話 還是得自己花一下時間熟悉一下 Python 才是 覺得妳 Python 似乎還不熟 就硬要處理一些數據... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.104.122.84 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1402415819.A.523.html ※ 編輯: Neisseria (59.104.122.84), 06/10/2014 23:58:41 ※ 編輯: Neisseria (59.104.122.142), 06/11/2014 00:25:02 ※ 編輯: Neisseria (59.104.122.142), 06/11/2014 00:28:11

06/11 00:28, , 1F
AttributeError: 'str' object has no attribute 'keys'
06/11 00:28, 1F

06/11 00:29, , 2F
恩恩 謝謝大大的提醒!似乎要馬上學飛超難的!
06/11 00:29, 2F

06/11 00:29, , 3F
我自己程式寫錯,噗~ 改過了,參考看看
06/11 00:29, 3F

06/11 00:30, , 4F
過這次應該會從基礎打起吧^^謝謝你 得熟悉才行阿~
06/11 00:30, 4F
文章代碼(AID): #1JbohBKZ (Python)
文章代碼(AID): #1JbohBKZ (Python)