将地理位置写入CSV fi

2024-04-26 01:01:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将这个geopy对象的输出写入csv文件,但它将每个字母放在不同的列中,并在不同的行上打印经度和纬度。我该怎么解决呢?在

我希望能够在不同的时间运行这个函数,并将新地址打印到下一行。保存数据而不是覆盖它。用python中的write csv可以做到这一点吗?在

from geopy.geocoders import Nominatim
import csv

def loc_find(file):
'''
This function take in a user given location and gives back the address with 
city, zip code, state, county and country.
It also provides latitude and longitude.
'''
    geolocator = Nominatim()
    loc_input = raw_input("Add the location you would like data back for: ")
    location = geolocator.geocode(loc_input)
    print(location.address)
    print((location.latitude, location.longitude))

    with open(r"", 'w') as fp:
        a = csv.writer(fp)
        data = location
        a.writerows(data)    

Tags: andcsvtheimportinputdataaddresswith