gpt4 book ai didi

python-3.x - python格式化问题解析子元素

转载 作者:行者123 更新时间:2023-12-01 03:03:02 25 4
gpt4 key购买 nike

我正在解析一个列出一些服务器的 xml 文件

2个服务器的示例xml:

<cluster-information>
<clustering-available>true</clustering-available>
<clustered>true</clustered>
<node>
<id>SomeIDnum</id>
<address>/someIP:port</address>
<local>false</local>
</node>
<node>
<id>SomeIDnum</id>
<address>/someIP:port</address>
<local>false</local>
</node>
</cluster-information>

我正在使用以下内容来获取 ID 和地址
cluster=myroot.find('cluster-information/clustered')
if cluster.text == 'true':
print("|Cluster is "+cluster.text+" |")
nodes=myroot.find('cluster-information')
for x in nodes.findall('node'):
id=x.find('id')
ip=x.find('address')
print("Node:"+id.text)
print("IP "+ip.text)
print("|")

结果是:

|集群为真 |
节点:someID
IP/x.x.x.x:端口
节点:someotherID
IP/x.x.x.x:端口
|

我需要输出看起来像这样:
|集群为真 |Node:someID
IP/x.x.x.x:端口
节点:someotherID
IP/x.x.x.x:端口
|

基本上我需要删除从 for 循环创建的第一个换行符。

最佳答案

I need to remove the first newline that is created form the for loop.



第一个换行符不是从 for 循环创建的 - 它是由
    print("|Cluster is "+cluster.text+" |")

在循环之前。要删除它,请更改为
    print("|Cluster is "+cluster.text+" |", end="")

关于python-3.x - python格式化问题解析子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59899764/

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