简体   繁体   English

在python中以正确格式输出XML

[英]XML output in proper format in python

The code is : 代码是:

results = ET.Element("results")
machine = ET.SubElement(results,"machine")
mac = ET.SubElement(machine, "mac")
ip = ET.SubElement(machine,"ip")
name = ET.SubElement(machine,"name")
download = ET.SubElement(machine, "download")
upload = ET.SubElement(machine, "upload")
comments = ET.SubElement(machine, "comments")

for line in lines.split("\n"):
         if 'MAC' in line:
                mac = line.split(":")
                mac.text = str(mac[1].strip())
        if 'IP' in line:
                ip = line.split(":")
                ip.text = str(ip[1].strip())
        if 'NAME' in line:
                name = line.split(":")
                name.text = str(name[1].strip())
        if 'Download' in line:
                down = line.split(":")
                download.text = str(down[1].strip())
        if 'Upload' in line:
                up = line.split(":")
                upload.text = str(up[1].strip())
        if 'Comments' in line:
                user = line.split(":")
                comments.text = str(user[1].strip())

tree = ET.ElementTree(results)
tree.write('machine.xml')

The Actual stdout output that need to be converted into xml is 需要转换为xml的Actual stdout输出为

MAC             : 00:19:ec;dc;bc
IP              : 192.111.111.111
NAME            : 900, Charles
Download        : 36MB
Upload          : 12MB
comments        : Since total througput is very less, we cannot continue

MAC             : 00:19:ac:bc:cd:
IP              : 192.222.222.222
NAME            : 800, Babbage
Download        : 36MB
Upload          : 24MB
comments        : Since total througput is high, we can continue

The Actual format I need to be generating is 我需要生成的实际格式是

<results>
   <machine>
     <MAC>00:19:ec;dc;bc</MAC>
     <ip>192.111.111.111</ip>
     <name>900, Charles</name>
     <upload>36MB</upload>
     <download>12MB</download>
     <comments>Since total througput is very less, we cannot continue</comments>
   </machine>
   <machine>
     <MAC>00:19:ac:bc:cd:</MAC>
     <ip>192.222.222.222</ip>
     <name>800, Babbage</name>
     <upload>36MB</upload>
     <download>24MB</download>
     <comments>Since total througput is high, we can continue</comments>
   </machine>
</results>

Output I am getting is 我得到的输出是

<results>
   <machine>
     <MAC>00:19:ec;dc;bc</MAC>
     <ip>192.111.111.111</ip>
     <name>900, Charles</name>
     <upload>36MB</upload>
     <download>12MB</download>
     <comments>Since total througput is very less, we cannot continue</comments>
   </machine>
<machine>
     <MAC>00:19:ec;dc;bc</MAC>
     <ip>192.111.111.111</ip>
     <name>900, Charles</name>
     <upload>36MB</upload>
     <download>12MB</download>
     <comments>Since total througput is very less, we cannot continue</comments>
   </machine>
</results>

I am using python 2.4(it is old but cannot upgrade currently) . 我正在使用python 2.4(它很旧,但目前无法升级)。 If somebody could suggest what is the mistake it would be great. 如果有人可以建议是什么错误,那就太好了。

Thanks ! 谢谢 !

You're only creating your child elements once, and changing their contents each time you run through the loop. 您只需创建一次子元素,并在每次循环中更改它们的内容。

Create the children inside the loop instead, each time you start to read a new machine. 每次您开始阅读新机器时,请在循环内创建子代。 Maybe have a sentinel outside the loop and reset it when you hit a blank line. 也许在循环外有一个哨兵,当您碰到空白行时将其重置。

You are only ever creating one instance of machine, which you are overwriting the contents of. 您只创建了一个计算机实例,您正在覆盖其中的内容。 Also the current code you have posted should throw the following error: 同样,您发布的当前代码应引发以下错误:

AttributeError: 'list' object has no attribute 'text'

To get around this you can create a new machine sub element each time you find a line that starts with "MAC". 为了解决这个问题,您可以在每次找到以“ MAC”开头的行时创建一个新的machine子元素。

keys = [
    "IP",
    "NAME",
    "Download",
    "Upload",
    "comments"
]

results = et.Element("results")
machines = []

for line in lines.split("\n":
    sp = line.split(" : ")
    try:
        key = sp[0].strip()
        val = sp[1].strip()
    except IndexError:
        continue

    if key == "MAC":
        machines.append(et.SubElement(results,"machine"))
        elem = et.SubElement(machines[-1],"mac")
        elem.text = val
    elif key in keys:
        elem = et.SubElement(machines[-1],key.lower())
        elem.text = val

tree = et.ElementTree(results)
tree.write("machine.xml")

This should give the desired output. 这将提供所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM