我需要从文件中创建一个Excel工作表。该文件具有这种格式,但是有数十个这样的模板。为了匹配整个文件的表示形式,可以将以下格式粘贴两次:
Field Value
OS-DCF:diskConfig MANUAL
OS-EXT-AZ:availability_zone az1
OS-EXT-STS:power_state 1
OS-EXT-STS:task_state None
OS-EXT-STS:vm_state active
OS-SRV-USG:launched_at 2016-02-04T12:53:35.000000
OS-SRV-USG:terminated_at None
accessIPv4
accessIPv6
addresses 10.10.10.10
config_drive True
created 2018-09-04T12:52:52Z
flavor m1.small
hostId ajsdajdad-qweqweqw-qwe123123-qweqweq-sadsadasd-1121212
id 922adwq-qwejjqeq-123123-asdasa
image cirros1
key_name None
name vm1
os-extended-volumes:volumes_attached []
progress 0
project_id id
properties ctrl='10.10.10.3', token='token', tenant='tenant1'
scheduler_hints {}
security_groups [{u'name': u'sg1'}, {u'name': u'sg2'}]
status ACTIVE
updated 2016-02-04T12:53:35Z
user_id user1
Excel文件应如下所示:
有问题的部分似乎在这里:
hostname, chains_segment = host.split('\n',1)
hostname = hostname.strip()
在剥离之后,我应该获得VM名称值,但是我得到的是空值。
这是完整的代码:
import xlsxwriter
import argparse
parser=argparse.ArgumentParser(description="Script")
parser.add_argument('-i','--input',help='Input log file name',required=True)
parser.add_argument('-o','--output',help='Desired name for the Excel file',required=True)
parser.add_argument('-s','--sheet',help='Desired name of the Excel sheet(Default: Sheet1)',default='Sheet1',required=False)
args=parser.parse_args()
az='| OS-EXT-AZ:availability_zone | '
state='| OS-EXT-STS:vm_state | '
launch='| OS-SRV-USG:launched_at | '
ipaddr='| addresses | '
flavor='| flavor | '
image='| image | '
def create_chain(chain_segment):
chains=[]
chain_lines = [line for line in chain_segment.split('\n') if line]
for line in chain_lines:
chain={}
if launch in line:
chain['launch'] = line.split()[3]
# chain['az'] = line.split()[3]
# chain['state'] = line.split()[3]
# chain['ipaddr'] = line.split()[3]
# chain['flavor'] = line.split()[3]
# chain['image'] = line.split()[3]
chains.append(chain)
chains=filter(None, chains)
chains=list(chains)
chained = [merge_dicts(chains[0], i) for i in chains[1:]]
return chained
def merge_dicts(x,y):
z=x.copy()
z.update(y)
return z
with open(args.input) as f:
log_content = f.read()
host_sections = [host for host in log_content.split(" Field Value") if host]
hosts = {}
for host in host_sections:
hostname, chains_segment = host.split('\n',1)
hostname = hostname.strip()
chains=[]
for segment in chains_segment.split('\n\n'):
chains.extend(create_chain(segment))
hosts[hostname] = chains
workbook=xlsxwriter.Workbook(args.output)
worksheet1=workbook.add_worksheet(args.sheet)
worksheet1.write(0,0,'VM')
worksheet1.write(0,1,'Availability Zone')
worksheet1.write(0,2,'State')
worksheet1.write(0,3,'Launched at')
worksheet1.write(0,4,'IP Address')
worksheet1.write(0,5,'Flavor')
worksheet1.write(0,6,'Image')
worksheet1.write(0,7,'Tenant')
worksheet1.write(0,8,'Security Group')
row = 1
for host, chains in hosts.items():
for chain in chains:
worksheet1.write(row, 0, host)
worksheet1.write(row, 1, chain.get('az'))
worksheet1.write(row, 2, chain.get('state'))
worksheet1.write(row, 3, chain.get('launch'))
worksheet1.write(row, 4, chain.get('ipaddr'))
worksheet1.write(row, 5, chain.get('flavor'))
worksheet1.write(row, 6, chain.get('image'))
row += 1
workbook.close()
有什么主意我可以解决这个问题吗?
非常感谢,
阿尔伯特
P.S。请注意,我是编程新手。