目前,我正在处理50,000行CSV表。这是我在API中输入的示例CSV表:
API然后处理每一行中的地址,并给我相应的坐标(纬度和经度)
我的问题是我需要在现有表中附加这些新的标题和值。 (在第[22]行说)
以下是我的Python查询:
import requests
import json
import pandas as pd
import numpy as np
import csv
import sys
from geocodio import GeocodioClient
import re
client = GeocodioClient('506be11563600404eb83151e40bb0f11ef06f3b')
# Input - CSV
df=pd.read_csv(r"C:\users\testu\documents\travis_50000_melissa_joined_dropna - Copy2.csv",delimiter=',', na_values="nan")
with open(r"C:\users\testu\documents\travis_50000_melissa_joined_dropna - Copy2.csv", 'a', newline='') as fp:
fieldnames = ["latitude","longitude","coordinates"]
writer = csv.DictWriter(fp, fieldnames=fieldnames)
writer.writeheader()
# Iterating requests for each row
for row in df.itertuples():
output = client.geocode(str(row.addressline1) + ', ' + str(row.city) + ', ' + str(row.state) + ', ' + str(row.postalcode)).coords
cord = '(' + str(output[0]) + ', '+ str(output[1]) + ')'
writer.writerow({'latitude': output[0], 'longitude': output[1], 'coordinates': cord})
print(output)
如下图所示,我在新行中获取API结果。
如何获得所需的CSV输出,如下图所示?
注意:请不要熊猫!我需要使用CSVwriter解决此问题。
答案 0 :(得分:1)
您需要将三个新字段附加到每个行,因此您需要将row
与这三个新字段合并。在Pandas中,这会有些混乱,因此请允许我使用pd.read_csv()
模块来重述csv
:
import csv
with open("test.csv") as in_file, open("test_out.csv", "w") as out_file:
csv_in = csv.DictReader(in_file, delimiter=",")
headers = csv_in.fieldnames + ["lat", "lon", "coord"]
csv_out = csv.DictWriter(out_file, fieldnames=headers)
csv_out.writeheader()
for row in csv_in:
output = client.geocode("{}, {}, {}, {}".format(
row["addressline1"],
row["city"],
row["state"],
row["postalcode"]
))
row["lat"] = output[0]
row["lon"] = output[1]
row["coord"] = "({} {})".format(output[0], output[1])
csv_out.writerow(row)
我强烈建议您创建一个 new 文件,而不仅仅是覆盖旧文件。