如何将地理编码结果写入CSV

时间:2018-10-02 21:33:04

标签: python geocode

我正在尝试使用地址解析模块,但无法弄清楚如何将结果写入文件。这是我到目前为止的内容。

我如何将该地址解析过程的输出写入output.csv?

import os

os.environ["GOOGLE_API_KEY"] = "api_key_from_google_cloud_platform"


import csv
import sys
import geocoder

output = '/Users/Username/Documents/Folder/output.csv'

def main(args):
    with open(args, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            data = [row['AddressLine1'], row['City'], row['State'], row['PostalCode'], row['CountryName']]
            separator = ', '
            joined_address = separator.join(data)
            print(joined_address)

            # Open the output file
            with open(output, 'w') as out:
                writer = csv.writer(out, lineterminator='\n')


            # Geocode them
            g = geocoder.google(joined_address)
            g.housenumber
            g.postal
            g.street
            g.street_long

            # Some kind of writer to my output file goes here?


if __name__ == '__main__':
    main(sys.argv[1])

1 个答案:

答案 0 :(得分:1)

应该这样做(假设您希望第一行成为标签)。

writer.writerow(["house_number", "postal", "street", "street_long"])
writer.writerow([g.housenumber, g.postal, g.street, g.street_long])

希望这会有所帮助!