1

xml file:

<Protocol1 channel="AljazeeraHD"> 

<frame source="vd01" id="4">

</frame>
<frame id="18" source="vd01">
   <rectangle id="1" height="87" width="976" y="889" x="414"/>   
</frame>
<frame id="23" source="vd01">
   <rectangle id="1" x="300" y="886" width="1095" height="89"/>   
</frame>
<frame id="35" source="vd01">
   <rectangle id="1" x="316" y="53" width="242" height="34"/>   
   <rectangle id="2" x="635" y="886" width="755" height="90"/>
</frame>
</Protocol1>

Want to access value of attribute 'x' from rectangle.

Code to convert xml_to _csv:

def xml_to_csv(path):

xml_list = []
for xml_file in glob.glob(path):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    for member in root.findall('frame'):
        frameNo = root.attrib['channel']+'_'+member.attrib['source']+'_frame_'+member.attrib['id']
        #print(member.find('rectangle').attrib['x'])
        #print(member.getchildren().attrib['x'])
        #print(member.find('rectangle').attrib['x'])
        value = (frameNo,
                 1920,
                 1080,
                 'Artificial',
                 int(member.find('rectangle').attrib['x']),                    
                 )
        xml_list.append(value)
print(glob.glob(path))
column_name = ['FrameNo', 'imgwidth', 'imgheight',
               'class','X']
xml_df = pd.DataFrame(xml_list, columns=column_name)
print(xml_list)        
return xml_df

Where 'member= frame' in a loop.

1.

member.getchildren().attrib['x'] 

AttributeError: 'NoneType' object has no attribute 'attrib'

2.

member.find('rectangle').attrib['x']

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

1
  • 1
    Please post a minimal reproducible example. Right now, it is not clear what packages/modules you are using. Commented May 13, 2021 at 12:30

1 Answer 1

1

Since you are using ElementTree and assuming your code looks like this:

members = """
<doc>
   <frame id="18" source="vd01">
      <rectangle id="1" height="87" width="976" y="889" x="414"/>
   </frame>
   <frame id="19" source="vd01">
      <rectangle id="1" height="87" width="976" y="889" x="333"/>
   </frame>
</doc>
"""

Then this should work:

import xml.etree.ElementTree as ET
doc = ET.fromstring(members)
for frame in doc.findall('.//frame'):         
       print(frame.attrib['x'])

Output:

414
333

EDIT:

Following your change to the xml, change the forloop to:

for frame in doc.findall('.//frame[rectangle]'):    
      for rec in frame.findall('.//rectangle'):
         print(rec.attrib['x'])

Now the output should be:

414
300
316
635
Sign up to request clarification or add additional context in comments.

3 Comments

It gives <Element 'rectangle' at 0x7f6f326af530> till member.find('rectangle') but when we add member.find('rectangle').attrib['x'] it says AttributeError: 'NoneType' object has no attribute 'attrib'.
@maryammehboob Then please edit your question and add a short, representative sample of the html you're working with.
JackFleeting, Thank you so much edited answer worked for me. I was missing inner for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.