#!/usr/bin/env python
import xlwt,xlrd,datetime,json,os,xlutils.copy
a={
'VpcPeeringConnection': {
'AccepterVpcInfo': {
'CidrBlock': 'abc',
'Ipv6CidrBlockSet': [
{
'Ipv6CidrBlock': 'qwe'
},
],
'CidrBlockSet': [
{
'CidrBlock': 'qwrqrqrwrqr'
},
],
'OwnerId': '123asf',
'PeeringOptions': {
'AllowDnsResolutionFromRemoteVpc': True,
'AllowEgressFromLocalClassicLinkToRemoteVpc': True,
'AllowEgressFromLocalVpcToRemoteClassicLink': True
},
'VpcId': 'asdfghjk',
'Region': ';lkjhg'
},
'ExpirationTime': "aaaa",
'RequesterVpcInfo': {
'CidrBlock': '12qwesd',
'Ipv6CidrBlockSet': [
{
'Ipv6CidrBlock': 'mjhngb'
},
],
'CidrBlockSet': [
{
'CidrBlock': 'oikujhg'
},
],
'OwnerId': 'lkjh',
'PeeringOptions': {
'AllowDnsResolutionFromRemoteVpc': True,
'AllowEgressFromLocalClassicLinkToRemoteVpc': True,
'AllowEgressFromLocalVpcToRemoteClassicLink': True
},
'VpcId': 'waqsd',
'Region': 'awsd'
},
'Status': {
'Code': "'initiating-request'",
'Message': 'lk_mjhn'
},
'Tags': [
{
'Key': 'mhngbv',
'Value': 'gfd'
},
],
'VpcPeeringConnectionId': 'ytgrf'
}
}
#os.remove('./bcd111.xls')
def write_excel(filename,dic):
sh = xlwt.Workbook()
sheet1 = sh.add_sheet('table123')
sheet1.write(0,0,"CidrBlock")
sheet1.write(0,1,"Ipv6CidrBlock")
sheet1.write(0,2,'OwnerId')
sheet1.write(0, 3, 'VpcId') dic_key = dic.keys()
temp =1
for key_names in dic_key:
key_name = a[key_names].keys()
for names in key_name:
if isinstance(a[key_names][names],dict):
if 'CidrBlock' in dic[key_names][names].keys() or 'Ipv6CidrBlock' in dic[key_names][names].keys() or 'OwnerId' in dic[key_names][names].keys() or 'VpcId' in dic[key_names][names].keys() :
sheet1.write(temp,0,dic[key_names][names].get('CidrBlock',''))
sheet1.write(temp, 1, dic[key_names][names].get('Ipv6CidrBlock',''))
sheet1.write(temp, 2, str(dic[key_names][names].get('OwnerId', '')))
sheet1.write(temp, 3, dic[key_names][names].get('VpcId', ''))
temp = temp+1
sh.save(filename) write_excel('bcd111.xls',a)
基于python2.7通过boto3实现ec2表格化,只做简单说明,后续更新