我需要能夠通過郵編搜索字典,但我不斷收到TypeError: sliced indices must be integers or None or have an __index__ method
。 我不確定如何整合__index__
方法。 這裏是我的代碼:從csv創建字典,需要密鑰是郵政編碼
import sys
import csv
import re
dicts = []
def getzip():
try:
f = open("zips.csv")
csvParser = csv.reader(f)
for row in csvParser:
dicts['zip code':row[0]] = {'latitude': row[2], 'longitude': row[3]}
print dicts
except ValueError:
pass
getzip()
如果我在dicts = {'zip code': row[1],'latitude': row[2], 'longitude': row[3]}
一切正常交換,但它打印Latitude:xxxxx zipcode:xxxxx longitude:xxxxx
,我需要它來按郵政編碼的結構。
有很多在'try'塊語句。儘可能保持'try'塊不變,這樣你就不會意外地忽略你不期望的異常。 – 2012-03-02 21:18:15
你打算存儲在「字典」,列表或字典從郵編到座標? – dsign 2012-03-02 21:19:41
郵政編碼與他們各自的座標,然後我需要編寫一個代碼來過濾通過在用戶輸入的郵政編碼的50英里內的郵編 – matture 2012-03-02 21:21:21