i started learning python recently and i have a problem with my code. i am trying to parse an xml with elementtree that with the following form:
<UNHEATED_ROWS>6</UNHEATED_ROWS>
<UNHEATED1 rid="1">
</UNHEATED1>
<UNHEATED2 rid="1">
</UNHEATED2>
<UNHEATED3 rid="1">
</UNHEATED3>
<UNHEATED4 rid="1">
</UNHEATED4>
<UNHEATED5 rid="1">
</UNHEATED5>
<UNHEATED6 rid="1">
with this:
for i in range root.iter('UNHEATED_ROWS'):
uheatedzones = UNHEATED_ROWS.text
for j in range uheatedzones:
uzones.append("UNHEATED" str(j 1)):
for k in range(int(uheatedzones)):
for uzones[k] in root.iter("UNHEATED%s" % (k 1)):
print(uzones[k])
Instead of UNHEATED1,UNHEATED2... that i expected to get with the print i get <Element 'UNHEATED1' at 0x0000017263D28220>
what is wrong with my code? thank you
CodePudding user response:
To get the text of a single element, don't use any loop. Use rather:
root.findtext('UNHEATED_ROWS')
Note however that the returned value is a string, not integer.
Another flaw in your code is str(j 1)
.
The first wrong thing is that j is an object of Element class and its conversion to string does not return its text.
The second wrong thing is j 1
, i.e. you attempt to add 1 to this object.
To process correctly each UNHEATED… element, you can e.g. use root.iter() (without parameters) and check in the loop whether the tag name starts with "UNHEATED" and then it contains a digit (to omit UNHEATED_ROWS element). You must import re module.
If you want to access attributes of an element, use attrib method of an element, which returns a dictionary ({attr_name: attr_value}).
So example loop can be:
i = 0
for it in root.iter():
if re.match(r'UNHEATED\d', it.tag):
i = 1
print(f'{i}: {it.tag}: {it.attrib["rid"]}')
For your source XML it prints:
1: UNHEATED1: 1
2: UNHEATED2: 1
3: UNHEATED3: 1
4: UNHEATED4: 1
5: UNHEATED5: 1
6: UNHEATED6: 1
Note also that I didn't use it.text
in the above loop, since each
UNHEATED… element has no text and in this case text method returns
None.
CodePudding user response:
thank you for your answer.
<UNHEATED1 rid="1">
<un_parameter1>82.8</un_parameter1>
<un_parameter2>123.3720</un_parameter2>
<ENVELOPE rid="1">
<opaque_rows>17</opaque_rows>
<transparent_column5>0.600,2.200,0.600,</transparent_column5>
<transparent_column6>Α1,Α2,Α10,</transparent_column6>
>
</ENVELOPE>
</UNHEATED1>
each UNHEATED has a similar stracture and i want to access for all UNHEATED, thats why i used the loop