Python 从csv创建字典,需要密钥为zipcode

Python 从csv创建字典,需要密钥为zipcode,python,Python,我需要能够通过zipcode搜索字典,但我不断得到一个类型错误:切片索引必须是整数或无,或者有一个方法。 我不知道如何集成\uuuu index\uuu方法。 这是我的密码: import sys import csv import re dicts = [] def getzip(): try: f = open("zips.csv") csvParser = csv.reader(f) for row in csvParser:

我需要能够通过zipcode搜索字典,但我不断得到一个
类型错误:切片索引必须是整数或无,或者有一个
方法。 我不知道如何集成
\uuuu index\uuu
方法。 这是我的密码:

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':行[1],'latitude':行[2],'longitude':行[3]}中进行交换

一切正常,但它会打印纬度:xxxxx zipcode:xxxxx经度:xxxxx,我需要用zipcode对其进行结构化。

这定义了一个列表,该列表由索引访问:

dicts = []
dicts[0] = 'something'
这将定义一个通过键访问的字典:

dicts = {} # curly braces
dicts['key'] = 'value'

我的猜测是,a
{}
就是你想要的。

这定义了一个列表,可以通过索引访问:

dicts = []
dicts[0] = 'something'
这将定义一个通过键访问的字典:

dicts = {} # curly braces
dicts['key'] = 'value'

我的猜测是,一个
{}
就是你想要的。

问题在于行
dicts[zip code:row[0]]
。您试图将列表当作字典来使用。

问题在于行
dicts[zip code:row[0]]
。您试图将列表当作字典来使用。

您的代码基本上是一个语法错误。您想用
dicts['zip code':第[0]行]
做什么

Python认为您正在使用slice操作符,就像您想要得到一个列表的中间部分一样,比如
some\u list[2:5]
(它从
some\u list的索引2到索引4返回项目)<代码>“邮政编码”
不能用作切片索引,因为它不是一个数字

我想你想做:

dicts = {}
通过使用
{}
声明
dicts
,它是一个字典,因此您可以使用邮政编码作为键

然后:

或许

     zip_code = row[0]
     dicts[zip_code] = {'zip code': zip_code, 'latitude': row[2], 'longitude': row[3]}
然后,您可以使用
dicts['91010']
访问邮政编码91010的信息:

>>> print dicts['91010']['latitude']
'-34.12N'

您的代码基本上是一个语法错误。您想用
dicts['zip code':第[0]行]
做什么

Python认为您正在使用slice操作符,就像您想要得到一个列表的中间部分一样,比如
some\u list[2:5]
(它从
some\u list的索引2到索引4返回项目)<代码>“邮政编码”
不能用作切片索引,因为它不是一个数字

我想你想做:

dicts = {}
通过使用
{}
声明
dicts
,它是一个字典,因此您可以使用邮政编码作为键

然后:

或许

     zip_code = row[0]
     dicts[zip_code] = {'zip code': zip_code, 'latitude': row[2], 'longitude': row[3]}
然后,您可以使用
dicts['91010']
访问邮政编码91010的信息:

>>> print dicts['91010']['latitude']
'-34.12N'
与此相反:

{'zip code': xxx, 'latitude': xxx, 'longitude': xxx } 
这样做:

{'xxx' : { 'latitude': xxx, 'longitude': xxx } } 
 #zipcode
与此相反:

{'zip code': xxx, 'latitude': xxx, 'longitude': xxx } 
这样做:

{'xxx' : { 'latitude': xxx, 'longitude': xxx } } 
 #zipcode

try
块中有很多语句。将
try
块尽可能小是个好主意,这样您就不会意外地忽略您不希望出现的异常。您打算在“dicts”、列表或字典中从邮政编码到坐标存储什么?带各自坐标的zipcodes,然后我需要写一个代码来过滤用户输入ZipCode的50英里范围内的ZipCode在你的
try
块中有很多语句。将
try
块尽可能小是个好主意,这样您就不会意外地忽略您不希望出现的异常。您打算在“dicts”、列表或字典中从邮政编码到坐标存储什么?带各自坐标的zipcodes,然后我需要编写一个代码来过滤用户输入的zipcode的50英里范围内的zipcode。我更新了我的答案,以便更清楚地解释为什么要使用
dicts={}
zipcode=row[0]。strip()dicts[zipcode]={'latitude':row[2]。replace(“,”).strip(),'longitude':row[3]。replace(“,”,”).strip())打印dicts['10306']['latitude']这是我当前的代码,但它给了我关键字错误:“10306'@Matthew我想你是想在解析带有10306邮政编码的CSV行之前打印
dicts['10306']
。@Matthew我把完整的测试代码放在了pastebin上。试试看。我更新了我的答案,以便更清楚地解释为什么要使用
dicts={}
zipcode=row[0]。strip()dicts[zipcode]={'latitude':row[2]。replace(“,”)。strip(),'longitude':row[3]。replace(“,”)。strip()}打印dicts['10306']['latitude']这是我当前的代码,但它给了我关键字错误:“10306”@Matthew我想你是想在解析带有10306邮政编码的CSV行之前打印
dicts['10306']
。@Matthew我把完整的测试代码放在了pastebin上。试试看。