使用csv和python 2.7,使用DictReader

时间:2016-08-23 02:43:40

标签: python-2.7 csv

我一直试图绕过这一段时间。我试图获取一个csv文件,提取所有行,连接2个值,取这两个值来计算距离csv的第三个值的距离,用csv存储距离和正确的数据,最后我需要找到最短的距离,并返回一个我尚未使用的所有值的字典。

    with open(filename,'r') as csvfile:
    reader = csv.DictReader(csvfile)
    #create a multi-dimensional dictionary with the store name as keyword
    new_dict = {}
    try:
        for row in reader:
            new_dict[row['name']] ={}
            new_dict[row['name']]['name'] = row['name']
            new_dict[row['name']]['dist'] = {}
            new_dict[row['name']]['address'] = row['address']
            new_dict[row['name']]['city'] = row['city']
            new_dict[row['name']]['state'] = row['state']
            new_dict[row['name']]['zip'] = row['zip']

            latt = str(row['latitude']) 
            longi = str(row['longitude'])
            #concantenate latt and longi for use in grate_circle distance calculation
            pharm_loc = latt + ','+ longi
            #add distance from usr_loc for each store to dict for each store
            new_dict[row['name']]['dist'] = str(calc_dist(usr_loc, store_loc))

我终于修复了这个部分,现在我需要帮助过滤掉除了最接近的结果以外的所有结果......我出于某种原因似乎无法理解这一点。任何帮助将不胜感激。

--- --- EDIT 现在正在运行的更新代码。这产生了一个多维字典如下...... {' CONTINENTAL':{' city':' TOPEKA'' dist':' 50.3131329882',& #39;姓名':' CONTINENTAL PHARMACY LLC'' zip':' 66603',' state':' KS','地址':' 821 SW 6TH AVE'},' DILLON':{' city':' ; TOPEKA'' dist':' 48.3573823197',' name&#39 ;:' DILLON PHARMACY',' zip&# 39;:' 66605','州':' KS','地址':' 2010 SE 29TH ST' }}

dict中有更多条目,我只需要过滤到最近的位置并仅返回该位置的值。

0 个答案:

没有答案