我想打印我的服务器。像这样:
Machine | Group | IP | Services
- Alpha | Public Server Group | 192.168.1.251 | JBoss, Tomcat
- Public | Public Server Group | 192.168.1.253 | JBoss, Tomcat
我的XML是:
^{pr2}$我试着这样做:
from xml.dom.minidom import parse
yXML = parse('/root/Desktop/gb/data/yConfig.xml')
print (' ')
print ('Machine | Group | IP | Services')
for AllConfigurations in yXML.getElementsByTagName('AllConfigurations'):
for DeployConfigurations in AllConfigurations.getElementsByTagName('DeployConfigurations'):
for Servers in DeployConfigurations.getElementsByTagName('Servers'):
for Group in Servers.getElementsByTagName('Group'):
for GApp in Group.getElementsByTagName('GApp'):
for Server in Group.getElementsByTagName('Server'):
print Server.getAttribute('name') + ' | ' + Group.getAttribute('name') + ' | ' + Server.getAttribute('ip') + ' | ' + GApp.getAttribute('type')
我的结果是:
Machine | Group | IP | Services
Alpha | Public Server Group | 192.168.1.251 | JBoss
Public | Public Server Group | 192.168.1.253 | JBoss
Alpha | Public Server Group | 192.168.1.251 | JBoss
Public | Public Server Group | 192.168.1.253 | JBoss
Alpha | Public Server Group | 192.168.1.251 | Tomcat
Public | Public Server Group | 192.168.1.253 | Tomcat
Alpha | Public Server Group | 192.168.1.251 | Tomcat
Public | Public Server Group | 192.168.1.253 | Tomcat
我该怎么办?在
所有可能的印刷品。我不能并排排列服务,不能为一个IP打印并且看起来像表。在
需要帮助。。在
我认为您需要利用python中提供的format specification mini-language:
上面脚本的输出是:
^{pr2}$首先,不能立即打印行;而是将数据(作为元组)存储在列表(
servers
)中。要按machine/group/IP对服务进行分组,可以使用itertools
函数groupby
,将前三个字段指定为键元组。(在此之前,必须对列表进行排序,以便groupby
查找所有重复项。)groupby
生成键(3元组)和其余相应行的生成器;这里,我们对第四个值(服务)的唯一值感兴趣,因此我们将这些值转换为set
,并用空格将它们连接起来。在通过使用
string
函数ljust
(左对齐)可以解决对齐表的问题。我创建了一个单独的函数来概括标题行和数据行的呈现。在代码如下:
这个指纹
^{pr2}$相关问题 更多 >
编程相关推荐