天天看点

python json转csv_使用Python将Json转换为CSV

下面是我从在线气象站提取的json结构.我还包括一个json_to_csv python脚本,该脚本应该将json数据转换为csv输出,但仅返回“ Key”错误.我只想从“ current_observation”中提取数据.

{

"response": {

"features": {

"conditions": 1

}

}

, "current_observation": {

"display_location": {

"latitude":"40.466442",

"longitude":"-85.362709",

"elevation":"280.4"

},

"observation_time_rfc822":"Fri, 26 Jan 2018 09:40:16 -0500",

"local_time_rfc822":"Sun, 28 Jan 2018 11:22:47 -0500",

"local_epoch":"1517156567",

"local_tz_short":"EST",

"weather":"Clear",

"temperature_string":"44.6 F (7.0 C)",

}

}

import csv, json, sys

inputFile = open("pywu.cache.json", 'r') #open json file

outputFile = open("CurrentObs.csv", 'w') #load csv file

data = json.load(inputFile) #load json content

inputFile.close() #close the input file

output = csv.writer(outputFile) #create a csv.write

output.writerow(data[0].keys())

for row in data:

output = csv.writer(outputFile) #create a csv.write

output.writerow(data[0].keys())

for row in data:

output.writerow(row.values()) #values row

检索温度字符串并将其转换为.csv格式的最佳方法是什么?谢谢!