how to parse XML with namespace and attribute in Python?

Question:

hi I am trying to parse xml with namespace and attribute.

I am almost close by using root.findall() and .get()

However still struggling to get the accurate values from xml file.

How to get the xml attribute values ?

Input:

<?xml version="1.0" encoding="UTF-8"?><message:GenericData 

root()
for x in root.findall('.//'):
    print(x.tag, " ", x.get('value'))

Output:

{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}Obs   None
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsDimension   1999-01
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsValue   0.7029125
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}Obs   None
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsDimension   1999-02
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsValue   0.688505

Expected_Output:

1999-01  0.7029125

1999-02  0.688505

Asked By: anonymus

||

Answers:

How about this:

for parent in root:                                                                 
    print('  '.join([child.get('value', "") for child in parent])) 
Answered By: DraftyHat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.