Python 2.7 to 3.6 Code porting issue --copying xml data to list

0

emphasized textHi , I am currently Porting a Piece of code from python 2.7 to python 3.6 , The Code involves dumping data from xml into a list the codes looks as below

 import os 
 import xml.etree.ElementTree as ET
 regs_list = []
 tree = ET.parse("test.xml")
 root = tree.getroot()
 for reg in root:
     regs_list.append(reg)
 for i in range(len(regs_list)):
     print (regs_list[i].attrib["name"])
     if not (regs_list[i].find("field") == None):
     for regs_list[i].field in regs_list[i]:
         print (regs_list[i].field.attrib["first_bit"])

The XML looks like this

 <register offset="0x4" width="4" defaultValue="0x100000" name="statuscommand" desc="STATUSCOMMAND- Status and Command ">
    <field first_bit="30" last_bit="31" WH="ROOO" flask="0xc0000000" name="reserved0" desc=""/>
    <field first_bit="29" last_bit="29" WH="1CWRH flask="0x20000000" name="rma" desc=""/>
    <field first_bit="28" last_bit="28" WH="1CWRH flask="0x10000000" name="rta" desc=""/>
 </register>
 <register offset="0x8" width="4" defaultValue="0x8050100" name="reve" desc="REVCLASSCODE - Revision ID and Class Code">
    <field first_bit="8" last_bit="31" WH="ROOO" flask="0xffffff00" name="class_codes" desc=""/>
    <field first_bit="0" last_bit="7" WH="ROOO" flask="0xff" name="rid" desc=""/>
 </register>

This Code works perfectly fine in python 2.7 , we are able to dump both parent (Register) and child (field) into the list regs_list , But in 3.6 we get an error as below

3.6 output " for regs_list[i].field in regs_list[i]: AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'field'

2.7 output Parent and child parsed and dumped in the list without error

Is there a difference in the way xml.etree.ElementTree.Element and lists work in 3.6 and 2.7 ??

python-3.x
python-2.7
asked on Stack Overflow Dec 12, 2019 by Krishna B Patil • edited Dec 12, 2019 by MisterMiyagi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0