我一直在尝试使用for循环来获取一些数据,然后将数据放入一个列表中,然后是dataframe。下面是我的代码:
quartiers = ['Centre-ville','Bellevue - Chantenay - Sainte-Anne','Dervallières - Zola','Hauts-Pavés - Saint-Félix','Malakoff - Saint-Donatien','Île de Nantes','Breil - Barberie', 'Nantes Nord', 'Nantes Erdre', 'Doulon - Bottiere', 'Nantes Sud']
quartier_coord = []
for quartier in quartiers:
address = '{}, Nantes, France'.format(quartier)
geolocator = Nominatim(user_agent="nantes_explorer_2")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print('The geograpical coordinates of {} are {}, {}.'.format(quartier, latitude, longitude))
quartier_coord = [{'Neighbourhood': quartier, 'Latitude': latitude, 'Longitude': longitude}]第一部分的输出是OK:
The geograpical coordinates of Centre-ville are 47.214839850000004, -1.557936644670939.
The geograpical coordinates of Bellevue - Chantenay - Sainte-Anne are 47.1977826, -1.5979483854260317.
The geograpical coordinates of Dervallières - Zola are 47.2177889, -1.5889576428457481.
The geograpical coordinates of Hauts-Pavés - Saint-Félix are 47.2287293, -1.5644036887311255.
The geograpical coordinates of Malakoff - Saint-Donatien are 47.223279399999996, -1.536068311104002.
The geograpical coordinates of Île de Nantes are 47.20704805, -1.5462102466548702.
The geograpical coordinates of Breil - Barberie are 47.2351552, -1.5738850630891967.
The geograpical coordinates of Nantes Nord are 47.258410350000005, -1.5663227883277482.
The geograpical coordinates of Nantes Erdre are 47.2649793, -1.5216089444961194.
The geograpical coordinates of Doulon - Bottiere are 47.23940985, -1.5094380518266286.
The geograpical coordinates of Nantes Sud are 47.192114000000004, -1.5324685615599893.但是列表中只有最后一行的数据:
print(quartier_coord)
['Nantes Sud', 47.192114000000004, -1.5324685615599893]我不知道我做错了什么。请给我建议。谢谢!
发布于 2021-06-29 21:16:33
您将在每次迭代中重新分配quartier_coord列表;您应该改为追加:
quartier_coord.append([{'Neighbourhood': quartier, 'Latitude': latitude, 'Longitude': longitude}])https://stackoverflow.com/questions/68179403
复制相似问题