【发布时间】:2019-08-23 14:27:05
【问题描述】:
我正在使用一个使用 JSON 文件响应的天气 API。以下是返回读数的示例:
{
'data': {
'request': [{
'type': 'City',
'query': 'Karachi, Pakistan'
}],
'weather': [{
'date': '2019-03-10',
'astronomy': [{
'sunrise': '06:46 AM',
'sunset': '06:38 PM',
'moonrise': '09:04 AM',
'moonset': '09:53 PM',
'moon_phase': 'Waxing Crescent',
'moon_illumination': '24'
}],
'maxtempC': '27',
'maxtempF': '80',
'mintempC': '22',
'mintempF': '72',
'totalSnow_cm': '0.0',
'sunHour': '11.6',
'uvIndex': '7',
'hourly': [{
'time': '24',
'tempC': '27',
'tempF': '80',
'windspeedMiles': '10',
'windspeedKmph': '16',
'winddirDegree': '234',
'winddir16Point': 'SW',
'weatherCode': '116',
'weatherIconUrl': [{
'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'
}],
'weatherDesc': [{
'value': 'Partly cloudy'
}],
'precipMM': '0.0',
'humidity': '57',
'visibility': '10',
'pressure': '1012',
'cloudcover': '13',
'HeatIndexC': '25',
'HeatIndexF': '78',
'DewPointC': '15',
'DewPointF': '59',
'WindChillC': '24',
'WindChillF': '75',
'WindGustMiles': '12',
'WindGustKmph': '19',
'FeelsLikeC': '25',
'FeelsLikeF': '78',
'uvIndex': '0'
}]
}]
}
}
我在尝试读取存储在 JSON 文件中的数据时使用了以下 Python 代码:
import simplejson as json
data_file = open("new.json", "r")
values = json.load(data_file)
但这会输出如下错误:
JSONDecodeError: Expecting value: line 1 column 1 (char 0) error
我还想知道如何使用 Python 将结果以结构化格式保存在 CSV 文件中。
【问题讨论】:
-
欢迎来到stackoverflow!请使用tour,阅读how to ask a question 并提供minimal, complete and verifiable example 来重现您的问题。
-
不清楚 csv 文件到底应该是什么样子