Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种方法可以循环使用此代码的列表?_Python_Loops_Google Maps - Fatal编程技术网

Python 有没有一种方法可以循环使用此代码的列表?

Python 有没有一种方法可以循环使用此代码的列表?,python,loops,google-maps,Python,Loops,Google Maps,基本上,我有一个大约5000个纬度和经度的列表,我想分别循环和运行这个查询。我一直在手工完成这项工作,并单独运行查询,但我认为必须有一种方法来标准化这个过程。非常感谢任何帮助或想法,谢谢 df = [] places_result = gmaps.places(query=query, location=(latitude1, longitude1), radius=32186.9, ) for place in places_result['results']: my_place_

基本上,我有一个大约5000个纬度和经度的列表,我想分别循环和运行这个查询。我一直在手工完成这项工作,并单独运行查询,但我认为必须有一种方法来标准化这个过程。非常感谢任何帮助或想法,谢谢

df = []

places_result = gmaps.places(query=query, location=(latitude1, longitude1), radius=32186.9, )

for place in places_result['results']:
    my_place_id = place['place_id']

    my_fields = ['name', 'business_status', 'formatted_address', 'opening_hours', 'rating', 'website',
                 'formatted_phone_number', 'geometry/location/lng', 'geometry/location/lat']

    place_details = gmaps.place(place_id=my_place_id, fields=my_fields)

    column_names = ['name', 'rating', 'address', 'website', 'phone', 'status', 'hours', 'lat', 'lng']

    # df=pd.DataFrame(columns = column_names)

    try:
        rating = place_details['result']['rating']
    except KeyError:
        rating = 'na'
    try:
        name = place_details['result']['name']
    except KeyError:
        name = "na"
    try:
        address = place_details['result']['formatted_address']
    except KeyError:
        address = "na"
    try:
        website = place_details['result']['website']
    except KeyError:
        website = "na"
    try:
        phone = place_details['result']['formatted_phone_number']
    except KeyError:
        phone = "na"
    try:
        lat = place_details['result']['geometry']['location']['lat']
    except KeyError:
        lat = "na"
    try:
        lng = place_details['result']['geometry']['location']['lng']
    except KeyError:
        lng = "na"
    try:
        status = place_details['result']['business_status']
    except KeyError:
        status = "na"
    try:
        hours = place_details['result']['opening_hours']
    except KeyError:
        hours = 'na'

    df1 = {"name": [name, ], "rating": [rating, ], "address": [address, ], "website": [website, ], "phone": [phone, ],
           "status": [status, ], "hours": [hours, ], "lat": [lat, ], "lng": [lng, ]}
    data: DataFrame = pd.DataFrame(data=df1)
    df.append(data)
dfr = pd.concat(df)

首先,关于您的尝试,除了块之外,您可以使用以下方法重写这些块:

result = place_details['result']

rating = result.get('rating', 'na')
name = result.get('name', 'na')
...
等等。 dict.get(key,default=None)方法将避免引发KeyError,您可以在本例中提供默认值“na”

您可以通过字典压缩来进一步细化:

keys = ["name", "rating", "address", "website", "phone",
        "status", "hours", "lat", "lng"]
entry = {k: result.get(k, 'na') for k in keys}
假设在两个列表中有lat和lon值:

entries = []
for lat, lon in zip(latitudes, longitudes):
    q = gmaps.places(query=query, location=(lat, lon), radius=32186.9,)
    result = q.get('result', {})  # returns an empty dict if the query fails
    entry = {k: result.get(k, 'na') for k in keys}
    entries.append(entry)

df = pd.DataFrame(entries)  # create a pandas dataframe
places_result = gmaps.places(query=query, location=(latitude1, longitude1), radius=32186.9)

def get_field_value(result, field):
  try:
    for f in field.split('/'):
      result = result[f]
    return result
  except KeyError:
    return 'na'

col_field = [ ('name', 'name'),
              ('rating', 'rating')
              ('address', 'formatted_address')
              ('website', 'website')
              ('phone', 'formatted_phone_number')
              ('status', 'business_status')
              ('hours', 'opening_hours')
              ('lat', 'geometry/location/lat')
              ('lng', 'geometry/location/lng')
            ]
fields = [e[1] for e in col_field]

for place in places_result['results']:
    place_details_result = gmaps.place(place_id=place['place_id'], fields=fields)['result']
    df1 = { column: [get_field_value(place_details_result, field)] for column, field in col_field }