简体   繁体   English

将 JSON API 结果导出到 CSV 文件

[英]Exporting JSON API result into a CSV file

I'm trying to export JSON API result into a CSV file.我正在尝试将 JSON API 结果导出到 CSV 文件中。 And I am having a formatting issue in CSV.我在 CSV 中有格式问题。

JSON API result 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': ' , , ,STE C-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': '', 'DeliveryPoi { '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= ' '记录':[{ 'AddressExtras': '', 'AddressKey': '78704,78704', 'AddressLine1':',,,STEç -100 ', 'AddressLine2': '1009 W MONROE ST ,1600 S 5TH ST ,1008 W MILTON ST ,3939 BEE CAVES RD', 'AddressTypeCode': '', 'BaseMelissaAddressKey': '', 'CarrierRoute': ,'城市':'奥斯汀,奥斯汀,奥斯汀,奥斯汀','城市缩写':'奥斯汀,奥斯汀,奥斯汀,奥斯汀','公司名称':'','国家代码':'美国','国家名称':'United状态', 'DeliveryIndi​​cator': '', 'DeliveryPoi ntCheckDigit': '', 'DeliveryPointCode': '', 'MelissaAddressKey': '', 'MoveEffectiveDate': '', 'MoveTypeCode': '', 'PostalCode': '78704,78704,78704,78746', 'RecordID': '1', 'Results': 'AE07', 'State': '', 'StateName': 'TX ,TX ,TX ,TX ', 'Urbanization': ''}], 'TotalRecords': '1', 'TransmissionReference': '1353', 'TransmissionResults': '', 'Version': '4.0.4.48'} [Finished in 2.6s] ntCheckDigit': '', 'DeliveryPointCode': '', 'MelissaAddressKey': '', 'MoveEffectiveDate': '', 'MoveTypeCode': '', 'PostalCode': '78704,78704,78704,78746', ': '1', 'Results': 'AE07', 'State': '', 'StateName': 'TX ,TX ,TX ,TX ', '城市化': ''}], 'TotalRecords': '1 ', 'TransmissionReference': '1353', 'TransmissionResults': '', 'Version': '4.0.4.48'} [2.6s 内完成]

Python query Python查询

r = response.json()


output_1 = []
output_1.append("AddressLine2")

# Collect only Address Line 2 from the JSON output. properly encode/decode the string and add it to output_1. 
for record in r['Records']:
    addressline2 = record['AddressLine2']
    addressline2.split(",")
    print(addressline2)
    output_1.append(addressline2)

print(output_1)

# Write the values to a column
with open(r"C:\users\testu\documents\travis_output.csv", 'w') as fp:
    writer = csv.writer(fp, dialect = 'excel')
    for val in output_1:
        writer.writerow([val])

Result I am getting结果我得到

在此处输入图片说明

Result I want结果我要

在此处输入图片说明

I thought addressline2.split(",") should do its job but it didn't work.我认为 addressline2.split(",") 应该完成它的工作,但它没有用。 Thanks for any help!谢谢你的帮助!

这会给你你的结果:

output_1 += addressline2.split(",") 

You are running addressline2.split(",") , but you aren't using the output of that operation in your CSV, instead you're just re-using addressline2 again.您正在运行addressline2.split(",") ,但您没有在 CSV 中使用该操作的输出,而是再次重新使用addressline2 The split method creates a new variable as the output of the function, it doesn't transform the original variable into a list. split 方法创建一个新变量作为函数的输出,它不会将原始变量转换为列表。

You need to capture the output, eg您需要捕获输出,例如

output_1.append(addressline2.split(","))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM