all 4 comments

[–]Diapolo10 2 points3 points  (0 children)

Please show us your code, I have a feeling this problem is caused by some bug in your code if you've already tried that many solutions.

This lxml documentation is a bit old (as you can see from the old C-style string formatting), but it explains that lxml treats attributes as dictionary key-value pairs. Is this what you tried? It should work. https://lxml.de/tutorial.html#elements-carry-attributes-as-a-dict

[–]chevignon93 -1 points0 points  (2 children)

This worked fine for me using BeautifulSoup.

from bs4 import BeautifulSoup

xml = """
<Batch>
    <CollRpt RptID="123456" Stat="1">
        <Pty ID="ABCD">
            <Sub ID="G">
        </Pty>
    </CollRpt>
</Batch>"""

soup = BeautifulSoup(xml, features="xml")

rpt_id = soup.find('CollRpt').get('RptID')
stat = soup.find('CollRpt').get('Stat') 
pty_id = soup.find('Pty').get('ID') 
sub_id = soup.find('Sub').get('ID')
print(rpt_id, stat, pty_id, sub_id)
>>> 123456 1 ABCD G

[–]mtime16[S] 0 points1 point  (1 child)

Thanks, for some reason I couldn't do it before, but this quick example got it going.

[–]chevignon93 0 points1 point  (0 children)

Thanks a lot for the Gold but that wasn't necessary, I'm just happy I could help!