2014-03-29 90 views
0

的代碼是:XML輸出在python

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(它是舊的,但目前無法升級)。如果有人可以提出什麼是錯誤,那將會很好。

謝謝!

+0

第一件事:「評論」!=「評論」。你也覆蓋整個地方的變量,'userexp','apidname','stnip'和'stnmac'不存在。 – ebarr

+0

而你得到的實際輸出是...? – sshashank124

+0

@ebarr根據實際代碼修改代碼。sshashank124:我已經更新輸出我得到 – NoviceInPython

回答

0

您只創建一次您的子元素,並且每次運行循環時更改其內容。

改爲在每次開始閱讀新機器時創建循環內的子項。也許在循環之外有一個標記,並在你打空行時重置它。

+0

如果我把循環內它不會給正確的結果,它打印 00:19:EC;直流; BC 192.111.111。111 NoviceInPython

+0

赦免,是的,你需要重新創建它們爲每個新_machine_,而不是每個新行。 – Eevee

0

你只有創建一個機器的實例,你正在覆蓋的內容。你也已經發布了當前的代碼應該拋出以下錯誤:

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

要解決這個問題,你可以創建一個新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") 

這應該給出所需的輸出。