我需要能够通过邮编搜索字典,但我不断收到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