之前我的一篇文章:
https://blog..net/zhangphil/article/details/88538112
是把从服务器接口读取到的json天气预报数据画制成折线图。现在把从json中的天气预报数据自动写入到Excel表格中。
需要安装Python处理excel表格的库:
pip install openpyxl
程序代码:
import openpyxl
import json
import urllib.request
def get_weather_json_data(city_code):
url = "http://t.weather.sojson.com/api/weather/city/"+city_code
response = urllib.request.urlopen(url)
content = response.read().decode('utf-8')
return content
def get_forecast_data(content):
data = content["data"]
return data["forecast"]
#清洗数据。过滤℃
def get_pure_temperature(temp):
a, b = temp.split()
return b.strip().strip("℃")
#数据写入到Excel。
def write_data_to_excel(date_time, high, low):
wb = openpyxl.load_