如何生成XML输出?

时间:2016-08-03 16:03:58

标签: python xml python-2.7 elementtree

是否可以将Python输出放入XML?

我到目前为止的代码是:

with open('logfile.txt', 'r') as StationLog:
    for line in StationLog:
        header = line.split('=')[0]
        if line.split('=')[0].strip() == 'name':
            name = line.split('=')[1].strip().lower()
            print name
        elif line.split('=')[0].strip() == 'siteIP':
            siteIP = line.split('=')[1].strip().rstrip('"').lstrip('"')
            print siteIP
        elif line.split('=')[0].strip() == 'id':
            id = line.split('=')[1].strip()
            print id

The ouput is:
abcd
192.168.1.1
123

我想要的输入是:

<Station>
    <name>abcd</name>
    <siteIP>192.168.1.1</siteIP>
    <id>123</id>
</Station>

0 个答案:

没有答案