json文件转Excel文件

#!/usr/bin/env python
# coding:utf-8
# author:YD
import json
import xlwt
import time

def each_chunk(streams, sep):
    buffer = ''
    while True:
        chunk = streams.read(10240)
        if not chunk:
            yield buffer
            break
        buffer += chunk
        while True:
            try:
                part, buffer = buffer.split(sep, 1)

            except ValueError:
                break
            else:
                yield part
def json_keys(json_path):
    with open(json_path, 'r', encoding='utf-8') as myFile:
        headers=[]
        for i, chunk in enumerate(each_chunk(myFile, sep='\n')):
            try:
                if chunk:
                    c = json.loads(chunk)
                else:
                    continue
                for header in c.keys():
                    if header not in headers:
                        headers.append(header)
            except Exception as e:
                print(chunk)
                print(e)
        return headers
def json_to_excel(json_path,headers):
    workbook_new = xlwt.Workbook()
    workbook_new.add_sheet("Sheet1")
    sheet_new = workbook_new.get_sheet("Sheet1")
    with open(json_path, 'r', encoding='utf-8') as myFile:
        count=0
        sheet=1
        headers=headers
        if not headers:
            headers = json_keys(json_path)
        for header_index,header in enumerate(headers):
            sheet_new.write(0, header_index, header)
        for i, chunk in enumerate(each_chunk(myFile, sep='\n')):
            try:
                if chunk:
                    c = json.loads(chunk)
                else:
                    continue
                for j, header in enumerate(headers):
                    if j == 0:
                        sheet_new.write(count + 1, j, c.get(header,''))
                    else:
                        sheet_new.write(count + 1, j, str(c.get(header,''))[0:30000] if c.get(header,'') else c.get(header,''))
            except Exception as e:
                print(chunk)
                print(e)
            count=count+1
            if count>=20000:
                workbook_new.save(f'{json_path.split(".")[0]}_{str(sheet)}.xls')
                workbook_new = xlwt.Workbook()
                workbook_new.add_sheet("Sheet1")
                sheet_new = workbook_new.get_sheet("Sheet1")
                for header_index, header in enumerate(headers):
                    sheet_new.write(0, header_index, header)
                count=0
                sheet=sheet+1
    workbook_new.save(f'{json_path.split(".")[0]}_{str(sheet)}.xls')

if __name__ == "__main__":
    # 需要转换成Excel的json文件路径
    json_path = r'C:\Users\admin\Desktop\66.json'
    # 需要转换和存储的
    # headers = ["id", "via", "created_at", "subject", "description", "assignee_name"]
    #headers为空,转换所有字段
    headers=[]
    start_time=time.time()
    json_to_excel(json_path, headers)
    end_time = time.time()
    print(end_time-start_time)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值