gpt4 book ai didi

python - 在 Python 中从 XML 打印数据以列表样式(表格)

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:36 26 4
gpt4 key购买 nike

我想按照列出的方式打印我的服务器。像这样:

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

需要帮助..

最佳答案

首先,你不能马上打印这些行;而是将数据(作为元组)存储在列表(服务器)中。要按机器/组/IP 对服务进行分组,您可以使用 itertools 函数 groupby,将前三个字段指定为键元组。 (在此之前,必须对列表进行排序,以便 groupby 找到所有重复项。)groupby 生成键(三元组)和其余对应行的生成器;在这里,我们对第四个值(服务)的唯一值感兴趣,因此我们将值转换为 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

关于python - 在 Python 中从 XML 打印数据以列表样式(表格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8528825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com