使用 Geopy 将地址列表转换为 GPS 坐标

Using Geopy to convert list of addresses to GPS coordinates

我知道如何使用 geopy 将单个地址转换为 GPS 坐标,但我的代码遇到了问题,它应该从文件中逐行读取地址并将每个地址转换为 GPS 坐标并打印出来。

from geopy.geocoders import Nominatim
geolocator = Nominatim()

f = open("FILE PATH")
line = f.readline()

for line in f.readlines():
    address = line
    location = geolocator.geocode(address)
    print((location.latitude, location.longitude))
f.close()

这对我有用。

from geopy import Nominatim
import time


geolocator = Nominatim()

with open("addresses",'r') as fp:
    for line in fp:
        location = geolocator.geocode(line)
        # print (location.latitude, location.longitude)
        time.sleep(1)