如何修复“字符串索引必须为整数”

时间:2019-04-08 21:03:48

标签: python

我正在使用Foursquare的JSON文件。而且我不断收到此错误“字符串索引必须是整数”

This is my dataset, county_merge
    county  density lat lng
0   Alameda 2532.292000 37.609029   -121.899142
1   Alpine  30.366667   38.589393   -119.834501
2   Amador  218.413333  38.449089   -120.591102
3   Butte   329.012500  39.651927   -121.585844
4   Calaveras   214.626316  38.255818   -120.498149
5   Colusa  393.388889  39.146558   -122.220956
6   Contra Costa    1526.334000 37.903481   -121.917535
7   Del Norte   328.485714  41.726177   -123.913280
8   El Dorado   444.043750  38.757414   -120.527613
9   Fresno  654.509259  36.729529   -119.708861
10  Glenn   477.985714  39.591277   -122.377866
11  Humboldt    392.427083  40.599742   -123.899773
12  Imperial    796.919048  33.030549   -115.359567
13  Inyo    127.561905  36.559533   -117.407471
14  Kern    608.326471  35.314570   -118.753822
15  Kings   883.560000  36.078481   -119.795634
16  Lake    608.338462  39.050541   -122.777656
17  Lassen  179.664706  40.768558   -120.730998
18  Los Angeles 2881.756000 34.053683   -118.242767
19  Madera  486.887500  37.171626   -119.773799
20  Marin   1366.937143 38.040914   -122.619964
21  Mariposa    48.263636   37.570148   -119.903659
22  Mendocino   198.010345  39.317649   -123.412640
23  Merced  1003.309091 37.302957   -120.484327
24  Modoc   100.856250  41.545049   -120.743600
25  Mono    133.145455  37.953393   -118.939876
26  Monterey    946.090323  36.600256   -121.894639
27  Napa    592.020000  38.297137   -122.285529
28  Nevada  338.892857  39.354033   -120.808984
29  Orange  1992.962500 33.750038   -117.870493
30  Placer  492.564000  39.101206   -120.765061
31  Plumas  87.817778   39.943099   -120.805952
32  Riverside   976.692105  33.953355   -117.396162
33  Sacramento  1369.729032 38.581572   -121.494400
34  San Benito  577.637500  36.624809   -121.117738
35  San Bernardino  612.176636  34.108345   -117.289765
36  San Diego   1281.848649 32.717421   -117.162771
37  San Francisco   7279.000000 37.779281   -122.419236
38  San Joaquin 1282.122222 37.937290   -121.277372
39  San Luis Obispo 627.285185  35.282753   -120.659616
40  San Mateo   1594.372973 37.496904   -122.333057
41  Santa Barbara   1133.525806 34.422132   -119.702667
42  Santa Clara 2090.724000 37.354113   -121.955174
43  Santa Cruz  1118.844444 36.974942   -122.028526
44  Shasta  180.137931  40.796512   -121.997919
45  Sierra  115.681818  39.584907   -120.530573
46  Siskiyou    202.170000  41.500722   -122.544354
47  Solano  871.818182  38.221894   -121.916355
48  Sonoma  926.674286  38.511080   -122.847339
49  Stanislaus  1181.864000 37.550087   -121.050143
50  Sutter  552.355556  38.950967   -121.697088
51  Tehama  206.862500  40.125133   -122.201553
52  Trinity 63.056250   40.605326   -123.171268
53  Tulare  681.425806  36.251647   -118.852583
54  Tuolumne    349.471429  38.056944   -119.991935
55  Ventura 1465.400000 34.343649   -119.295171
56  Yolo    958.890909  38.718454   -121.905900
{'meta': {'code': 200, 'requestId': '5cab80f04c1f6715df4e698d'},
 'response': {'venues': [{'id': '4b9bf2abf964a520573936e3',
    'name': 'Bishop Ranch Veterinary Center & Urgent Care',
    'location': {'address': '2000 Bishop Dr',
     'lat': 37.77129467449237,
     'lng': -121.97112176203284,
     'labeledLatLngs': [{'label': 'display',
       'lat': 37.77129467449237,
       'lng': -121.97112176203284}],
     'distance': 19143,
     'postalCode': '94583',
     'cc': 'US',
     'city': 'San Ramon',
     'state': 'CA',
     'country': 'United States',
     'formattedAddress': ['2000 Bishop Dr',
      'San Ramon, CA 94583',
      'United States']},
    'categories': [{'id': '4d954af4a243a5684765b473',
      'name': 'Veterinarian',
      'pluralName': 'Veterinarians',
      'shortName': 'Veterinarians',
      'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/building/medical_veterinarian_',
       'suffix': '.png'},
      'primary': True}],
    'venuePage': {'id': '463205329'},
    'referralId': 'v-1554743537',
    'hasPerk': False},
   {'id': '4b9acbfef964a5209dd635e3',
    'name': 'San Francisco SPCA Veterinary Hospital',
    'location': {'address': '201 Alabama St',
     'crossStreet': 'at 16th St.',
     'lat': 37.766633450405465,
     'lng': -122.41214303998395,
     'labeledLatLngs': [{'label': 'display',
       'lat': 37.766633450405465,
       'lng': -122.41214303998395}],
     'distance': 48477,
     'postalCode': '94103',
     'cc': 'US',
     'city': 'San Francisco',
     'state': 'CA',
     'country': 'United States',
     'formattedAddress': ['201 Alabama St (at 16th St.)',
      'San Francisco, CA 94103',
      'United States']},
    'categories': [{'id': '4d954af4a243a5684765b473',
      'name': 'Veterinarian',
      'pluralName': 'Veterinarians',
      'shortName': 'Veterinarians',
      'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/building/medical_veterinarian_',
       'suffix': '.png'},
      'primary': True}],
    'referralId': 'v-1554743537',
    'hasPerk': False},
   {'id': '4b00d8ecf964a5204d4122e3',
    'name': 'Pleasanton Veterinary Hospital',
    'location': {'address': '3059B Hopyard Rd Ste B',
     'lat': 37.67658,
     'lng': -121.89778,
     'labeledLatLngs': [{'label': 'display',
       'lat': 37.67658,
       'lng': -121.89778}],
     'distance': 7520,
     'postalCode': '94588',
     'cc': 'US',
     'city': 'Pleasanton',
     'state': 'CA',
     'country': 'United States',
     'formattedAddress': ['3059B Hopyard Rd Ste B',
      'Pleasanton, CA 94588',
      'United States']},
    'categories': [{'id': '4d954af4a243a5684765b473',
      'name': 'Veterinarian',
      'pluralName': 'Veterinarians',
      'shortName': 'Veterinarians',
      'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/building/medical_veterinarian_',
       'suffix': '.png'},
      'primary': True}],
    'referralId': 'v-1554743537',
    'hasPerk': False},

这是我正在处理的JSON文件。而且,我想吸引姓名,纬度,经度和城市名称。

results = requests.get(url).json()
results

names=county_merge['county']
la=county_merge['lat']
ln=county_merge['lng']

venues_list = []       
venues_list.append([(
     names, 
     la, 
     ln, 
     v['response']['venues'][0]['name'],
     v['response']['venues'][0]['location']['lat'],
     v['response']['venues'][0]['location']['lng'],
     v['response']['venues'][0]['location']['city']) for v in results])

我希望这会给我几行内容。 [县名,38.xxxx,-120.xxxx,XXX兽医诊所,38.xxxx,-120.xxxx,圣地亚哥] [县名,38.xxxx,-120.xxxx,XXX兽医诊所,38.xxxx,-120.xxxx,圣地亚哥] [县名,38.xxxx,-120.xxxx,XXX兽医诊所,38.xxxx,-120.xxxx,圣地亚哥] [县名,38.xxxx,-120.xxxx,XXX兽医诊所,38.xxxx,-120.xxxx,圣地亚哥] [县名,38.xxxx,-120.xxxx,XXX兽医诊所,38.xxxx,-120.xxxx,圣地亚哥] 。 。 。

但是,这只会给我带来错误和沮丧。


TypeError                                 Traceback (most recent call last)
<ipython-input-44-321b1c667727> in <module>
     11      v['response']['venues'][0]['location']['lat'],
     12      v['response']['venues'][0]['location']['lng'],
---> 13      v['response']['venues'][0]['location']['city']) for v in results])

<ipython-input-44-321b1c667727> in <listcomp>(.0)
     11      v['response']['venues'][0]['location']['lat'],
     12      v['response']['venues'][0]['location']['lng'],
---> 13      v['response']['venues'][0]['location']['city']) for v in results])

TypeError: string indices must be integers

您有解决此代码的想法吗?

1 个答案:

答案 0 :(得分:2)

[v for v in results]

给你

['meta', 'response']

因此,您得到了结果键,即字符串。我想你要

venues_list.append([(
 names, 
 la, 
 ln, 
 v['name'],
 v['location']['lat'],
 v['location']['lng'],
 v['location']['city']) for v in results['response']['venues'])
相关问题