将Geopy位置写入CSV文件

时间:2016-03-09 20:25:36

标签: python csv geopy

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

我希望能够在不同时间运行此功能并将新地址打印到下一行。保存数据不会覆盖它。可以在python中编写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)    

1 个答案:

答案 0 :(得分:0)

好吧,您将Location对象作为rows(行对象列表)传递,您应该将其作为单行传递。

替换:

a.writerows(location)

使用:

a.writerow(location)