在python open csv中的字典中建立列表

时间:2018-09-23 01:34:25

标签: python loops dictionary

嗨,我正在尝试建立一本字典,其中包含一个每天较长的清单。

请查看数据示例:

  date            latitude          longitude
1/01/2012          135.5             149.5
1/01/2012          138.8             151.5
3/02/2012          134.7             143.8

理想情况下,日期将是字典中的键,而经纬度则是元组列表。

  {'1/01/12' : [(135.5 , 149.5),(138.8 ,151.5)] ,'3/02/2012': [(134.7, 143.8)]}         

请在下面查看我的代码,问题是它仅添加了位置之一。

with open("file", 'r') as f:
    reader = csv.reader(f, delimiter = ',')
    headers = next(reader)
    accidents_daily = []
    for line in f:
        line = line.strip().split(',')
        date = line[1]
        location = (line[4],line[5])##represents my lat and long

        locations_daily[crash_date] = (location)

原始数据示例:

CRASH_ID,CRASH_DATE,CRASH_TIME,SUBURB_LOCATION,LONGITUDE,LATITUDE,INTERSECTION,MIDBLOCK,CRASH_DIRECTION,CRASH_SEVERITY,LIGHTING_CONDITION,ROAD_CONDITION,WEATHER_CONDITION,Location
1143355,31/08/2016,15:20,WARAMANGA,149.0646898,-35.34840034,NO,YES,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.348400344091395, 149.06468975103948)"
1191616,23/08/2018,18:15,PHILLIP,149.0848095,-35.34886707,NO,YES,West bound,Property Damage Only,Dark - good street lighting,Good dry surface,Cloudy or Overcast,"(-35.34886707244341, 149.08480946267707)"
1120068,9/09/2015,11:15,TURNER,149.1298864,-35.27342246,YES,NO,North bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.273422461411066, 149.12988641734273)"
1080387,28/01/2014,8:30,TURNER,149.1306254,-35.26040749,YES,NO,East / West bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.26040749341896, 149.13062535940057)"
1076245,19/11/2013,10:00,CAMPBELL,149.1473753,-35.282155,YES,NO,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.282155004486896, 149.14737534275548)"
1109287,15/04/2015,17:30,BELCONNEN,149.0710302,-35.23817334,NO,YES,North bound,Property Damage Only,Daylight,Wet surface,Cloudy or Overcast,"(-35.238173337249656, 149.07103019391346)"
1152056,27/01/2017,13:42,YARRALUMLA,149.1070649,-35.30479792,YES,NO,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.30479791661196, 149.10706487573066)"
1030906,28/02/2012,6:30,BRUCE,149.0887509,-35.25094292,YES,NO,South bound,Property Damage Only,Dark - good street lighting,Wet surface,Light rain,"(-35.250942917963485, 149.0887508999686)"
1119850,7/09/2015,12:10,RURAL - TUGGERANONG,149.1365865,-35.39028997,YES,NO,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.39028997205093, 149.1365865448212)"
1061849,18/05/2013,12:30,KINGSTON,149.1441428,-35.31463096,NO,YES,North bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.314630964653354, 149.14414282152543)"
1059943,2/04/2013,11:00,CAMPBELL,149.1551892,-35.28166816,NO,YES,West bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.281668158228086, 149.15518920244364)"
1071721,15/09/2013,13:20,WESTON,149.0522999,-35.34143873,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.341438728406914, 149.05229989973964)"
1089318,20/06/2014,7:50,STIRLING,149.0568903,-35.34779082,YES,NO,North / East bound,Injury,Daylight,Good dry surface,Fine,"(-35.347790821523894, 149.05689028104578)"
1164570,20/07/2017,13:30,BRADDON,149.1433425,-35.27602195,YES,NO,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.276021954576805, 149.14334246747734)"
1057813,15/03/2013,17:00,CITY,149.1306045,-35.27593626,NO,YES,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.275936262233344, 149.13060450922876)"
1026206,3/01/2012,7:30,RURAL - CANBERRA CENTRAL,149.0881433,-35.2589105,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.258910500452345, 149.08814331874453)"
1083445,15/03/2014,16:14,RURAL - TUGGERANONG,149.1170637,-35.38322518,YES,NO,West bound,Property Damage Only,Daylight,Good dry surface,Cloudy or Overcast,"(-35.383225176666116, 149.1170637438708)"
1162844,21/06/2017,10:05,GUNGAHLIN,149.1208645,-35.18561166,NO,YES,East bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.185611661788485, 149.12086450236487)"
1107684,20/03/2015,16:30,YARRALUMLA,149.1067534,-35.31091769,YES,NO,East / South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.310917694935426, 149.10675337583916)"
1130929,2/03/2016,16:50,RURAL - MOLONGLO VALLEY,149.0765117,-35.2912999,YES,NO,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.291299904539166, 149.07651168464156)"
1047290,25/09/2012,9:28,LYONS,149.0659711,-35.34019838,NO,YES,South bound,Property Damage Only,Daylight,Good dry surface,Fine,"(-35.34019837683168, 149.0659710656289)"

2 个答案:

答案 0 :(得分:2)

在将结果添加到locations_daily时,如果.append()已经存在,则必须crash_date新建位置。这应该起作用:

    if crash_date in locations_daily:
        locations_daily[crash_date].append(location)
    else:
        locations_daily[crash_date] = [location]

答案 1 :(得分:0)

还请注意,您实际上并未在脚本中使用csv.reader()。使用类似这样的方法来利用 csv解析器,而不是用逗号分隔行:

import csv

locations_daily={}

with open("data.csv") as f:
    reader = csv.reader(f, delimiter = ',')
    headers = reader.next()
    while True:
        try:
            line = reader.next()
        except StopIteration:
            break

        crash_date = line[1]
        location = (float(line[4]), float(line[5]))   # Also converting the lat/long to float

        if crash_date in locations_daily:
            locations_daily[crash_date].append(location)
        else:
            locations_daily[crash_date] = [location]

print(locations_daily)