#!/usr/bin/python3
#防止出现中文乱码
# -*- coding:utf-8 -*-
import json
import tablib
import os
import time
#数据表
##数据表 定义表头
header1 = (
‘Time Stamp‘,
‘Source IP‘,
‘Destination IP‘,
‘Source Port‘,
‘Destination Port‘,
‘Protocol‘,
‘Traffic Flow‘,
‘Traffic Decision‘,
‘Flow State‘,
‘Packets‘,
‘Bytes sent‘,
‘Packets‘,
‘Bytes sent‘
)
##数据表 数据
#打开json文件并序列化
with open(‘2.json‘,‘r‘,encoding=‘utf8‘)as fp:
json_data = json.load(fp)
data1 = []
for json_s1 in json_data[‘records‘]:
for json_s2 in json_s1[‘properties‘][‘flows‘][0][‘flows‘][0][‘flowTuples‘]:
# print(json_s2)
json_s2 = json_s2.split(‘,‘)
b = int(json_s2[0])
#转换成localtime
time_local = time.localtime(b)
#转换成新的时间格式(2016-05-05 20:28:54)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
# print(dt)
#新时间格式替换原数据
json_s2[0] = dt
data1.append(json_s2)
#制作表格
dataset1 = tablib.Dataset(*data1, headers = header1)
# print(‘dataset1: ‘, os.linesep, dataset1, os.linesep)
#导出到Excel表格中
dataset1.title = ‘NSG‘ #设置Excel中表单的名称
myfile = open(‘NSG.xlsx‘, ‘wb‘)
myfile.write(dataset1.xlsx)
myfile.close()