将JSON API结果导出到CSV文件

时间:2018-10-29 15:12:35

标签: python json api

我正在尝试将JSON API结果导出到CSV文件。而且我在CSV中遇到格式问题。

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','DeliveryIndi​​cator':'','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秒内完成]

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])

我得到的结果

enter image description here

我想要的结果

enter image description here

我认为addressline2.split(“,”)应该可以完成工作,但是没有用。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

这将为您提供结果:

output_1 += addressline2.split(",") 

答案 1 :(得分:0)

您正在运行const blacklist = require('metro-config/src/defaults/blacklist'); module.exports = { resolver: { blacklistRE: blacklist([/react-native\/local-cli\/core\/__fixtures__.*/]), sourceExts: process.env.RN_SRC_EXT ? process.env.RN_SRC_EXT.split(',') : [] } }; ,但是您没有在CSV中使用该操作的输出,而是再次使用了sourceExtssplit method创建一个新变量作为函数的输出,它不会将原始变量转换为列表。

您需要捕获输出,例如

addressline2.split(",")
相关问题