[英]How to get output in proper json format in this piece of code in python
[英]XML output in proper format in 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的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
我需要生成的實際格式是
<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(它很舊,但目前無法升級)。 如果有人可以建議是什么錯誤,那就太好了。
謝謝 !
您只需創建一次子元素,並在每次循環中更改它們的內容。
每次您開始閱讀新機器時,請在循環內創建子代。 也許在循環外有一個哨兵,當您碰到空白行時將其重置。
您只創建了一個計算機實例,您正在覆蓋其中的內容。 同樣,您發布的當前代碼應引發以下錯誤:
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")
這將提供所需的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.