从stdout读取python的XML生成

时间:2014-03-28 20:54:31

标签: python xml linux web

我将stdout放在下面的表单中,我需要为以下输出生成xml> 如何在python中生成xml。 我在循环中读取了stdout的子元素。但它没有生成正确的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

我需要以下格式的xml

<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 = 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")
comment = ET.SubElement(machine, "comment")

for line in lines.split("\n"):
         if 'MAC' in line:
                mac = line.split(":")
                stnmac.text = str(mac[1].strip())
        if 'IP' in line:
                ip = line.split(":")
                stnip.text = str(ip[1].strip())
        if 'NAME' in line:
                name = line.split(":")
                apidname.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 'Comment' in line:
                user = line.split(":")
                userexp.text = str(user[1].strip())

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

在xml中重复相同的属性,如果我放入循环子元素中,每个机器在xml中只有一个属性。

2 个答案:

答案 0 :(得分:0)

您可以使用BeautifulSoup。我们的想法是将字符串拆分为换行符(或逐行读取源文件),并在每个空行上创建一个machine标记。在每个非空行上,先将行拆分为:,然后创建一个标记并附加到machine标记。

以下是您可以开始使用的工作示例:

from bs4 import Tag


data = """
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 = Tag(name='results')

machine = None
for line in data.split('\n'):
    if not line:
        if machine:
            results.append(machine)
        machine = Tag(name='machine')
    else:
        name, value = line.split(':', 1)
        tag = Tag(name=name.strip())
        tag.string = value.strip()
        machine.append(tag)

print results.prettify()

打印:

<results>
 <machine>
  <MAC>
   00:19:ec;dc;bc
  </MAC>
  <IP>
   192.111.111.111
  </IP>
  <NAME>
   900, Charles
  </NAME>
  <Download>
   36MB
  </Download>
  <Upload>
   12MB
  </Upload>
  <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>
  <Download>
   36MB
  </Download>
  <Upload>
   24MB
  </Upload>
  <comments>
   Since total througput is high, we can continue
  </comments>
 </machine>
</results>
PS:我不喜欢“分组”部分 - 看起来不像pythonic,但是很有效。

希望有所帮助。

答案 1 :(得分:0)

以下是使用xml.etree.ElementTree的解决方案:

import xml.etree.ElementTree as ET

data = """
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 = ET.Element('results')

machine = None
for line in data.split('\n'):
    if not line:
        machine = ET.SubElement(results, 'machine')
    else:
        name, value = line.split(':', 1)
        tag = ET.SubElement(machine, name.strip())
        tag.text = value.strip()

print ET.tostring(results)

打印:

<results>
    <machine>
        <MAC>00:19:ec;dc;bc</MAC>
        <IP>192.111.111.111</IP>
        <NAME>900, Charles</NAME>
        <Download>36MB</Download>
        <Upload>12MB</Upload>
        <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>
        <Download>36MB</Download>
        <Upload>24MB</Upload>
        <comments>Since total througput is high, we can continue</comments>
    </machine>
    <machine/>
</results>