利用Python将api接入的数据写入excel

162 阅读1分钟

引入相应的包

import jsonpath
import json
import xlwt

打开json文件所在位置

f = open("文件名.json", encoding="utf-8")

构建空字典

if __name__ == '__main__':
    time=[]
    ...
    ...
    ...

运用jsonpath.jsonpath获取字典数据、 路径

time=jsonpath.jsonpath(jsondata, '$..time')

过遍历方式将当天天气数据写入字典,使其符合写入excel格式规范

for i in range(len(time)):
    res = [time[i]...]
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('TEST11', cell_overwrite_ok=True)
col = ('time'...)

取数并写入excel

for i in range(0, 23):
    sheet.write(0, i, col[i])

for i in range(len(ans)):
    data = ans[i]
    for j in range(0, 23):
        sheet.write(i+1, j, data[j])

savepath = r'文件存储位置.xls'
book.save(savepath)