python中格式正确的XML输出

时间:2014-03-29 01:40:11

标签: python xml linux

代码是:

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')

需要转换为xml的实际标准输出

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

我需要生成的实际格式是

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

我得到的输出是

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

我正在使用python 2.4(它已经过时但目前无法升级)。如果有人可以提出什么是错误,那就太棒了。

谢谢!

2 个答案:

答案 0 :(得分:0)

您只需创建一次子元素,并在每次循环时更改其内容。

每次开始阅读新机器时,在循环内创建子项。也许在循环外有一个哨兵,并在你打个空行时重置它。

答案 1 :(得分:0)

你只是创建了一个机器实例,你要覆盖它的内容。您发布的当前代码也应该抛出以下错误:

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

为了解决这个问题,每次找到以“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")

这应该提供所需的输出。