Start New Discussion within our Software Development Community

hi,

i'm using Element Tree API to generate XML. But I'm getting error when opening the xml output. I tried to generate to a file and it worked. No error when running the Python code. Pls advise me why is not workin when i generate to an xml. fyi i'm using Python 2.5 and Win XP.
My code:

import elementtree.ElementTree as ET

lspeed=2
tspeed=3
f=open("out.txt", "w")
xml=open(("C:\\Python25\\myscript\\cmm\\port2.xml","a")

for port in range(1,9):
         
         print "Port %d" %port
         root = ET.Element("Test")
         f.write("Port %d\n" %port)
         head1 = ET.SubElement(root, "Default_Config", Port=str(port))
         xml.write(head1)
         print "Link speed: %d" %lspeed
         f.write("Link speed: %d\n" %lspeed)
         title = ET.SubElement(head1, "LINK")
         title.text = str(lspeed)
         xml.write(title)
         print "Target speed: %d" %tspeed
         f.write("Target speed: %d\n" %tspeed)
         title = ET.SubElement(head1, "Target Speed")
         title.text = str(tspeed)
         tree = ET.ElementTree(root)
         xml.write(title)
         tree.write("C:\\Python25\\myscript\\cmm\\port1.xml")

f.close()
xml.close()

thanks
tcl
p/s: attached is my xml output and text file

Attachments
Port 1
Link speed: 2
Target speed: 3
Port 2
Link speed: 2
Target speed: 3
Port 3
Link speed: 2
Target speed: 3
Port 4
Link speed: 2
Target speed: 3
Port 5
Link speed: 2
Target speed: 3
Port 6
Link speed: 2
Target speed: 3
Port 7
Link speed: 2
Target speed: 3
Port 8
Link speed: 2
Target speed: 3

hi,

i managed to solve the error by removing the space in btwe Target Speed by putting an underscore (Target_Speed). However the output is not what i want. it overwrites everytime the port num is iterated. how can i write to xml to be like below:

Port 1
Link speed: 2
Target speed: 3
Port 2
Link speed: 2
Target speed: 3
Port 3
Link speed: 2
Target speed: 3
Port 4
Link speed: 2
Target speed: 3
Port 5
Link speed: 2
Target speed: 3
Port 6
Link speed: 2
Target speed: 3
Port 7
Link speed: 2
Target speed: 3
Port 8
Link speed: 2
Target speed: 3

thanks
tcl

This article has been dead for over six months. Start a new discussion instead.