将数据打印到Python中用XML列出样式(表)

时间:2011-12-16 01:30:41

标签: python xml list tabular

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

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是:

<?xml version="1.0" ?>
<AllConfigurations>

<DeployConfigurations>

    <Servers>

        <Group id="1" name="Public Server Group" username="root" password="mypasswd123" state="">
            <GApp id="1" name="JBoss Servers" type="JBoss" path="/root/Desktop/jboss-as-7.0.2.Final/" state="">
                <Server id="1" name="Alpha" ip="192.168.1.251" path="/root/Desktop/jboss-as-7.0.2.Final/" username="" password="" state="" />
                <Server id="2" name="Public" ip="192.168.1.253" path="/root/Desktop/jboss-as-7.0.2.Final/" username="" password="" state="" />
            </GApp>
            <GApp id="2" name="Tomcat Servers" type="Tomcat" path="/root/Desktop/apache-tomcat-7.0.22/" state="">
                <Server id="1" name="Alpha" ip="192.168.1.251" path="/root/Desktop/apache-tomcat-7.0.22/" username="" password="" state="" />
                <Server id="2" name="Public" ip="192.168.1.253" path="/root/Desktop/apache-tomcat-7.0.22/" username="" password="" state="" />
            </GApp>
        </Group>

    </Servers>

</DeployConfigurations>

</AllConfigurations>

我试图这样做:

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并看起来像表。

需要帮助..

2 个答案:

答案 0 :(得分:4)

首先,你不能马上打印线条;而是将数据(作为元组)存储在列表中(servers)。要按机器/组/ 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)))

打印

Machine  | Group                | IP              | Services
- Alpha  | Public Server Group  | 192.168.1.251   | JBoss, Tomcat
- Public | Public Server Group  | 192.168.1.253   | Tomcat, JBoss

答案 1 :(得分: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)

上面脚本的输出是:

Machine              | Group                | IP                   | Services
             Machine |                Group |                   IP |             Services
      Machine        |        Group         |          IP          |       Services