#!/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)
json文件转Excel文件
于 2023-02-13 10:27:37 首次发布