代码
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import json
import tablib
# 获取json数据
rows = [{"a": 1}, {"a": 2}, {"a": 3}, {"a": 4}, {"a": 5}]
# 将json中的key作为header, 也可以自定义header(列名)
header = tuple([i for i in rows[0].keys()])
# header = tuple(["a"])
data = []
# 循环里面的字典,将value作为数据写入进去
for row in rows:
body = []
for v in row.values():
body.append(v)
data.append(tuple(body))
data = tablib.Dataset(*data, headers=header)
with open('guangdiantong.xls', "wb") as f:
f.write(data.xls)
参考 https://blog.csdn.net/gllaxq/article/details/81487712