我正在尝试将JSON API结果导出为格式正确的CSV。
注意:SmartMover API可以通过输入个人地址,名字,姓氏等来帮助确定个人地址是否最新。
以下是我的JSON格式的API结果。
{'CASSReportLink':'https://smartmover.melissadata.net/v3/Reports/CASSReport.aspx?tkenrpt=YvBDs39g52jKhLJyl5RgHKpuj5HwDMe1pE2lcQrczqRiG3/3y5yMlixj5S7lIvLJpDyAOkD8fE8vDCg56s3UogNuAkdTbS2aqoYF5FvyovUjnXzoQaHaL8TaQbwyCQ2RB7tIlszGy5+LqFnI7Xdr6sjYX93FDkSGei6Omck5OF4=','NCOAReportLink':'https://smartmover.melissadata.net/v3/Reports/NCOAReport.aspx?tkenrpt=8anQa424W7NYg8ueROFirapuj5HwDMe1pE2lcQrczqRiG3/3y5yMlixj5S7lIvLJpDyAOkD8fE8vDCg56s3UogNuAkdTbS2aqoYF5FvyovUjnXzoQaHaL8TaQbwyCQ2RB7tIlszGy5+LqFnI7Xdr6sjYX93FDkSGei6Omck5OF4=','Records':[{'AddressExtras':'','AddressKey':'78704,78704' ,'AddressLine1':',,,STEC-100','AddressLine2':'1009 W MONROE ST,1600 S 5TH ST,1008 W MILTON ST,3939 BEE CAVES RD','AddressTypeCode':'','BaseMelissaAddressKey ':'','CarrierRoute':'','City':'Austin,Austin,Austin,Austin','CityAbbreviation':'Austin ,Austin,Austin,Austin','CompanyName':``,'CountryCode':'US','CountryName':'United States','DeliveryIndicator':'','DeliveryPointCheckDigit':'','DeliveryPointCode': '','MelissaAddressKey':'','MoveEffectiveDate':'','MoveTypeCode':'','PostalCode':'78704,78704,78704,78746','RecordID':'1','Results': 'AE07','State':'','StateName':'TX,TX,TX,TX','城市化':''}],'TotalRecords':'1','TransmissionReference':'1353', 'TransmissionResults':'','Version':'4.0.4.48'} [在2.6秒内完成]
从此结果中,我想在AddressLine2标头中提取4个输出值。 (我在API中输入了4个值)例如,
'Records':...............'AddressLine2':'1009 W MONROE ST',............
所以我编写了以下Python查询:
r = response.json()
df = pd.read_csv(r"C:\users\testu\documents\travis_test.csv",delimiter = ',',na_values="nan")
with open(r'C:\users\testu\documents\travis3.csv', 'w+') as f:
cf = csv.writer(f)
cf.writerow(r['Records'][0]['AddressLine2'].split(','))
print(r['Records'][0]['AddressLine2'].split(','))
但是,它只能提取没有标题和水平的值,如下图所示:
如何使用Header(AddressLine2)垂直提取值?
感谢您的帮助!
答案 0 :(得分:0)
最近我不得不做类似的事情。我最终还是使用了另一种方法,而没有使用熊猫。
我假设您正在使用python请求库,csv库,pandas和python 3。
假设您只尝试获取标题为AddressLine2的一列,则应执行以下操作:
import requests
import unicodedata
import csv
url = "The API endpoint"
# Build an array to contain the Address values. Add the header AddressLine2
# by appending the string to output_1.
output_1 = []
output_1.append("AddressLine2")
# Get the information from the API using requests.
response = requests.get(url)
data = response.json()
# Collect only Address Line 2 from the JSON output. properly encode/decode the
# string and add it to output_1.
for record in data['records']:
addressline2 = record['addressline2']
decode_1 = unicodedata.normalize('NFKD', addressline2)
output_1.append(decode_1)
# Write the values to a column
with open(csvfile, 'w') as fp:
writer = csv.writer(fp, dialect = 'excel')
for val in output_1:
writer.writerow([val])
我知道我没有为此使用熊猫。因此,如果您需要将该熊猫数据框用于其他计算,则可能会花费过多的额外工作。但是,如果没有,那将有望为您带来专栏!