python读取json 生成Excel表格

import json
import xlwt

biaotou='[{"name_cn":"企业名称","name_en":"QYMC"},{"name_cn":"类别","name_en":"LB"},{"name_cn":"地市","name_en":"DS"},{"name_cn":"批次","name_en":"PC"}]';
datas='[{"PC":"第一批","LB":"专精特新工业","QYMC":"某某有限公司","DS":"济南"}]'

def getDate():
    print("来萨博 biaotou  datas")

    # 创建一个workbook 设置编码
    workbook = xlwt.Workbook(encoding='utf-8')
    # 创建一个worksheet
    worksheet = workbook.add_sheet('My Worksheet1')

    mes_to_dict = json.loads(biaotou)
    for index,item, in enumerate(mes_to_dict):
        # 参数对应 行, 列, 值
        worksheet.write(1, index, label=item['name_cn'])
    datas_json = json.loads(datas)
    for  index,item in enumerate(datas_json):
        print(index,item)
        worksheet.write(index+2, 0, label=item['PC'])
        worksheet.write(index+2, 1, label=item['LB'])
        worksheet.write(index+2, 2, label=item['QYMC'])
        worksheet.write(index+2, 3, label=item['DS'])




    # 保存
    workbook.save('Excel_test2.xls')
if __name__ == '__main__':
    getDate();

你可能感兴趣的:(PyThon,python,json,excel)