简体   繁体   中英

Get data from XML using xml.etree - python

I was wondering if anyone could help me with a situation, I am trying to get some values from an xml request. But, I was not successful, if anyone has any idea about it. Please share it with me....

This is my code so far,

cost_centre = []

for book in xml.iter("unit"):
    try:
        if book.attrib["parentCode"] == 'MHI' or 'MHEU':
#             print(book.attrib['parentCode'])
#               print(book.attrib)
            if book.attrib["categoryName"] == "Cost Centre":
                cost_centre.append((book.attrib['parentCode'],book.attrib['oracleCode']))
    except:
        book = ''
# print(cost_centre)
df = pd.DataFrame(cost_centre,columns=['entity','oracleCode'])
display(df)

   entity   oracleCode
0   00174   STR-0134
1   00126   1120100101
# -------------------------------

What I want is...
   entity   oracleCode
0   MHI     STR-0134
1   MHI     1120100101
#Convert to XML
xml = ET.fromstring(emberRaw)
cost_centre = []

for book in xml.iter("unit"):
    if book.get("parentCode") == 'MHI' or book.get("parentCode") == 'MHEU':
        mhi_mheu =  book.get("parentCode")
    if book.get("categoryName") == "Cost Centre":
        oracle = book.get("oracleCode")
    cost_centre.append((mhi_mheu,oracle))
    if cost_centre == '':
        cost_centre.remove()
        
cost_centre = list(set(cost_centre))
print(len(cost_centre))

df = pd.DataFrame(cost_centre,columns=['entity','oracleCode'])
display(df)

 entity    oracleCode
0   MHI    9999999987
1   MHI    1220200201
2   MHI    1110100102
3   MHEU   1220301804

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM