写json文本字符串到excel

# -*- coding: utf-8 -*-

from xmindparser import xmind_to_dict
import xlwt
import json
import pprint
# 记录列数,全局变量,还原方便
columnIndex = 0
# 记录行数
rowIndex = 1
# 记录任务个数
taskCount = 0


if __name__ == '__main__':
    # 用例地址
    file_path = 'json.txt'

    # 使用xlwt模块
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('task', cell_overwrite_ok=True)


    title_list = ['任务ID', '任务名称']

    # 用例标题写入excel首行
    for j in range(0, len(title_list)):
        ws.write(0, j, title_list[j])
        j += 1

    # 读取文本

    with open(file_path, "r") as f:
        data_json = json.loads(f.read())
        print('-------------')
        print(type(data_json))
        pprint.pprint(data_json)


    for j in range(0, len(data_json)):
        task_id = data_json[j]['id']
        task_name = data_json[j]['name']

        taskCount += 1  #任务个数
        ws.write(rowIndex, columnIndex, task_id)   #写任务id
        ws.write(rowIndex, columnIndex + 1, task_name)  # 写任务name
        j += 1

        rowIndex += 1

    print('任务总数%s:' % taskCount)

    # 保存Excel文档
    wb.save('QtaTaskList.xls')

  

posted @ 2022-03-31 10:47  ReluStarry  阅读(116)  评论(0编辑  收藏  举报