简体   繁体   English

使用 xml.etree 从 XML 获取数据 - python

[英]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.我想知道是否有人可以帮助我解决问题,我正在尝试从 xml 请求中获取一些值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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