import xlrd
from collections import OrderedDict
import json
import codecs
wd = xlrd.open_workbook('test.xlsx')
convert_list = []
sh = wd.sheet_by_index(0)
title = sh.row_values(0)
num = 1
for rownum in range(1, sh.nrows):
rowvalue = sh.row_values(rownum)
single = OrderedDict()
for colnum in range(0, len(rowvalue)):
single[title[colnum]] = rowvalue[colnum]
convert_list.append(single)
j = json.dumps(convert_list, ensure_ascii=False)
with codecs.open('file.json',"w","utf_8_sig") as f:
f.write(j)
代码功能:excel第一行为json的key;将每一行保存为一个json,将所有行保存为json的数组
简单说明:j = json.dumps(convert_list, ensure_ascii=False) 确保写入的中文可以被直接打开显示为中文