各国首都经纬度python
时间: 2023-08-27 10:17:39 浏览: 130
你可以使用Python中的geopy库来获取各国首都的经纬度。以下是一个获取部分国家首都经纬度的示例代码:
```python
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="my_app")
capitals = {
"China": "Beijing",
"United States": "Washington, D.C.",
"Japan": "Tokyo",
"United Kingdom": "London"
}
for country, capital in capitals.items():
location = geolocator.geocode(capital)
print(f"{capital} ({country}): ({location.latitude}, {location.longitude})")
```
输出结果为:
```
Beijing (China): (39.906217, 116.3912757)
Washington, D.C. (United States): (38.8949924, -77.0365581)
Tokyo (Japan): (35.6828387, 139.7594549)
London (United Kingdom): (51.5073219, -0.1276474)
```
你可以添加更多国家和首都到`capitals`字典中来获取更多的首都经纬度。
阅读全文