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

How to write geocode results to csv

我正在尝试使用地理编码模块,但我不知道如何将结果写入文件。以下是我目前所拥有的。

如何将该地理编码过程的输出写入 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])

这应该可以做到(假设您希望第一行是标签)。

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

希望对您有所帮助!