#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : 123.py
# @Author: gaojian
# @Date : 2017/12/19
# @Desc :
import json,re,xlwt
import simplejson
def read_file(filename):
with open(filename) as f:
line = f.read().replace("'", '"')
records = json.loads(line)
return records
def get_keys(content):
result = []
result_temp = {}
for key_name in content.keys():
if key_name == "Reservations":
for second in content.get(key_name):
if isinstance(second,dict):
result_temp={}
for second_name in second.keys():
if second_name == "OwnerId":
result_temp["OwnerId||||0"] = second.get("OwnerId","")
elif second_name == "Instances":
for second_value in second.get(second_name):
if isinstance(second_value,dict):
for third in second_value.keys():
if third == "Placement":
result_temp['AvailabilityZone||||19'] = second_value.get(third).get('AvailabilityZone','')
result_temp["Monitoring||||1"] = second_value.get("Monitoring","")
result_temp["PublicDnsName||||2"] = second_value.get("PublicDnsName","")
result_temp["EbsOptimized||||3"] = second_value.get("EbsOptimized","")
result_temp["LaunchTime||||4"] = second_value.get("LaunchTime","")
result_temp["PrivateIpAddress||||5"] = second_value.get("PrivateIpAddress","")
result_temp["VpcId||||6"] = second_value.get("VpcId","")
result_temp["InstanceId||||7"] = second_value.get("InstanceId","")
result_temp["ImageId||||8"] = second_value.get("ImageId","")
result_temp["PrivateDnsName||||9"] = second_value.get("PrivateDnsName","")
result_temp["KeyName||||10"] = second_value.get("KeyName","")
result_temp["SecurityGroups||||11"] = second_value.get("SecurityGroups","")
result_temp["SubnetId||||12"] = second_value.get("SubnetId","")
result_temp["InstanceType||||13"] = second_value.get("InstanceType","")
result_temp["SourceDestCheck||||14"] = second_value.get("SourceDestCheck","")
result_temp["Hypervisor||||15"] = second_value.get("Hypervisor","")
result_temp["Architecture||||16"] = second_value.get("Architecture","")
result_temp["RootDeviceType||||17"] = second_value.get("RootDeviceType","")
result_temp["VirtualizationType||||18"] = second_value.get("VirtualizationType","")
result.append(result_temp)
return result
def write_excel(filename,content):
if not isinstance(content,list):
return -1
sh = xlwt.Workbook()
write_sh = sh.add_sheet('name1')
t = 0
for temp in content:
if t == 0:
for temp_key in temp.keys():
nclos = temp_key.split('||||')[1]
keys = temp_key.split('||||')[0]
write_sh.write(t,int(nclos),keys)
for temp_key in temp.keys():
nclos = temp_key.split('||||')[1]
write_sh.write(t+1,int(nclos),str(temp.get(temp_key)))
t=t+1
sh.save(filename)
if __name__ == "__main__":
filename = "./a.txt"
execl_filename = "./result.xls"
content = read_file(filename)
result = get_keys(content)
write_excel(execl_filename,result)
# a='''{'Reservations': [{'Instances': [{'Monitoring': {'State': 'disabled'}, 'PublicDnsName': '', 'StateReason': {'Message': 'Client.UserInitiatedShutdown: User initiated shutdown', 'Code': 'Client.UserInitiatedShutdown'}, 'State': {'Code': 80, 'Name': 'stopped'}, 'EbsOptimized': False'''
# print len(a)