Python中将数据从XML打印到列表样式(表格)

2024-10-01 07:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打印我的服务器。像这样:

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打印并且看起来像表。在

需要帮助。。在


Tags: inipalphaforservergrouppublicmachine
2条回答

我认为您需要利用python中提供的format specification mini-language

data = ('Machine', 'Group', 'IP', 'Services')
# left aligned (default)                                                                                                                  
print '{0:20} | {1:20} | {2:20} | {3:20}'.format(*data)
# rigth aligned (default)                                                                                                                 
print '{0:>20} | {1:>20} | {2:>20} | {3:>20}'.format(*data)
# centered                                                                                                                                
print '{0:^20} | {1:^20} | {2:^20} | {3:^20}'.format(*data)

上面脚本的输出是:

^{pr2}$

首先,不能立即打印行;而是将数据(作为元组)存储在列表(servers)中。要按machine/group/IP对服务进行分组,可以使用itertools函数groupby,将前三个字段指定为键元组。(在此之前,必须对列表进行排序,以便groupby查找所有重复项。)groupby生成键(3元组)和其余相应行的生成器;这里,我们对第四个值(服务)的唯一值感兴趣,因此我们将这些值转换为set,并用空格将它们连接起来。在

通过使用string函数ljust(左对齐)可以解决对齐表的问题。我创建了一个单独的函数来概括标题行和数据行的呈现。在

代码如下:

from itertools import groupby

servers = []
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'):
                        servers.append((Server.getAttribute('name'),
                                Group.getAttribute('name'),
                                Server.getAttribute('ip'),
                                GApp.getAttribute('type')))

def line(machine, group, ip, services):
    return " | ".join([machine.ljust(8), group.ljust(20), ip.ljust(15), services])

print line("Machine", "Group", "IP", "Services")
for server, services in groupby(sorted(servers), lambda server: server[0:3]):
    print line("- " + server[0], server[1], server[2],
            ", ".join(service[3] for service in set(services)))

这个指纹

^{pr2}$

相关问题 更多 >