代码之家  ›  专栏  ›  技术社区  ›  twelsh37

在函数之间传递字典值

  •  0
  • twelsh37  · 技术社区  · 5 年前

    Python 3.8,windows 10

    大家好,我是Python新手,所以请友好一点:)

    我在与busAPI交互的代码编写的最后阶段遇到了问题。(不,我不是公交车观察员)

    到目前为止,我已经从用户那里获取了数据

    已验证该服务是我们支持的服务

    检索到有关服务的相关数据

    检索了服务的路线信息,并将其传递给最后一部分,我打算在那里绘制地图。

    不幸的是,它只绘制了一个点,即终点

    我可以看到我的函数在交互,因为我有大量的调试日志记录,所以我可以看到函数之间的数据流等。

        ....
        ....
        1
        DEBUG 4: Alyssum Walk , 51.89186 , 0.93168
        DEBUG 5: Alyssum Walk , 51.89186 , 0.93168
        DEBUG 5.1: map_it_dict{'stop': 'Alyssum Walk', 'latitude': 51.89186, 'longitude': 0.93168}
                   stop  latitude  longitude
        0  Alyssum Walk  51.89186    0.93168
        1
        DEBUG 4: Azalea Court , 51.89278 , 0.93334
        DEBUG 5: Azalea Court , 51.89278 , 0.93334
        DEBUG 5.1: map_it_dict{'stop': 'Azalea Court', 'latitude': 51.89278, 'longitude': 0.93334}
                   stop  latitude  longitude
        0  Azalea Court  51.89278    0.93334
        1
        DEBUG 4: Library , 51.89165 , 0.93706
        DEBUG 5: Library , 51.89165 , 0.93706
        DEBUG 5.1: map_it_dict{'stop': 'Library', 'latitude': 51.89165, 'longitude': 0.93706}
              stop  latitude  longitude
        0  Library  51.89165    0.93706
        1
        DEBUG 0: Returned from  bus_route(). Last Stop is Library , 51.89165 , 0.93706
    

    如上所述,我在所有函数和Pandas数据帧中都获得了一致的数据。

    调试0是主要功能

    调试4-来自我的函数busroute。这将查询一个URL并返回路线上的停靠站。这是与公交车站名称以及公交车站的纬度和长度一起交付的。busroute函数的最后一部分将bustop、lat和long传递给Map_it函数

    调试5向我们展示了它正在从Bus_route函数接收相同的数据。最后,在第19行,您可以看到pandas数据帧,同样具有相同的信息。

    )是我pandas数据框架中的索引 1是当时词典中的项目数(总路径为53个项目)

    我的map_it函数中的数据应该在一张活页地图上绘制52个公交车站。但它只是映射了最后一个。

    Folium map showing only one marker, should be 53

    我认为这是因为传递给map_it函数的数据是逐行传递的,我怀疑每次字典都被重写了。因此,数据报的索引为0。

    我在bus_route函数中获取所有数据

        What bus do you want?: 64
        The number 64 is a bus service we support
        DEBUG 0: From validate_bus() we got 64
        DEBUG 0: From bus_service(): Bus Number 64 , Traveling from: Greenstead, Essex, Traveling to: Shrub End, Essex
        DEBUG 4: Hazell Avenue , 51.87154 , 0.86659
        DEBUG 4: Paxman Avenue , 51.87186 , 0.86827
        DEBUG 4: Alderman Blaxhill School , 51.87236 , 0.87038
        ...
        ...
        ...
        DEBUG 4: Alyssum Walk , 51.89186 , 0.93168
        DEBUG 4: Azalea Court , 51.89278 , 0.93334
        DEBUG 4: Library , 51.89165 , 0.93706
        DEBUG 0: Returned from  bus_route(). Last Stop is Library , 51.89165 , 0.93706
    

    这是我为这两个函数编写的代码。

        def bus_route(bus_number):
            # This function queries the transport API for route data for a specific bus service number
            # It receives input as bus_number from main()
            # it provides bus_stand, lat and long of each of the stops along the specific buses route
            # and passes them to map_it() for route mapping.
    
            bus = bus_number
            # Retrieve a URL via urllib3
            # This could be tidied up using data from URL but for expediancy it is coded in here
            # Use %s to pass in the Constants and Variables to make up the URL
            if bus == "64":
                url = BASE_URL + '/route/FESX/%s/inbound/1500IM2349B/2020-05-22/06:40/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            elif bus == "65":
                url = BASE_URL + '/route/FESX/%s/inbound/1500IM2456B/2020-05-22/19:27/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            elif bus == "67":
                url = BASE_URL + '/route/FESX/%s/inbound/150033038003/2020-05-22/06:55/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            elif bus == "70":
                url = BASE_URL + '/route/FESX/%s/inbound/1500IM77A/2020-05-22/06:51/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            elif bus == "74B":
                url = BASE_URL + '/route/FESX/%s/inbound/15003303800B/2020-05-22/20:10/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            elif bus == "88":
                url = BASE_URL + '/route/FESX/%s/inbound/1500IM77A/2020-05-22/05:50/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
            else:
                url = BASE_URL + '/route/FESX/%s/inbound/1500IM52/2020-05-22/06:00/timetable.json?app_id=%s&app_key=%s' \
                                 '&edge_geometry=false&stops=ALL' % (bus, APP_ID, API_KEY)
    
            http = urllib3.PoolManager()
    
            # Request our data, and decode the json data returned
            response = http.request('GET', url)
            bus_route_dict = json.loads(response.data.decode('utf-8'))
            x = 0
            # iterate through our dictionary giving us the bus stop names and their
            # lat and long so we can plot them on a map.
            #while x < len(bus_route_dict['stops']):
            #    print(x)
            for stop in bus_route_dict['stops']:
                bus_stand = stop['stop_name']
                lat = stop['latitude']
                long = stop['longitude']
                print("DEBUG 4: " + bus_stand + " , " + str(lat) + " , " + str(long))
                #map_it(bus_stand, lat, long)
            return bus_stand, lat, long
    
        def map_it(bus_stand, lat, long):
            # This function maps teh bus route on a folium map
            # It receives input as bus_stand, lat, and long from bus_route
    
            # Folium mapping
            stop = bus_stand
            latitude = lat
            longitude = long
    
            # DEBUG code to show we are receiving code from get_route()
            print("DEBUG 5: " + stop + " , " + str(latitude) + " , " + str(longitude))
    
            # Setup a dictionary to store the information we need to build
            # a folium map
            map_it_dict = {}
            map_it_dict['stop'] = stop
            map_it_dict['latitude'] = latitude
            map_it_dict['longitude'] = longitude
            print("DEBUG 5.1: map_it_dict" + str(map_it_dict))
    
            # lets get the dict into pandas
            map_it_df = pd.DataFrame([map_it_dict])
            # Check we got data - we get it. tw 22/05/2020
            #print(map_it_df.head())
    
            # Prep data for the map
            locations = map_it_df[['latitude', 'longitude']]
            locationlist = locations.values.tolist()
            print(len(locationlist))
    
            # Now build the map
            # the Location is the Lat/Long for Colchester
            route_64 = folium.Map(location=[51.8959,0.8919] , zoom_start=14)
            for point in range(0, len(locationlist)):
                folium.Marker((locationlist[point]) , popup=map_it_dict['stop']).add_to(route_64)
                route_64.save("route_maps/route_64.html ")
    

    我看不出在哪里创建我的字典,因为我在bus_route()函数中得到的似乎都是单行。我意识到,如果我能把打印语句附加到字典中,那么我的问题就解决了。

    0 回复  |  直到 5 年前