簡體   English   中英

使用 xml.etree 從 XML 獲取數據 - python

[英]Get data from XML using xml.etree - python

我想知道是否有人可以幫助我解決問題,我正在嘗試從 xml 請求中獲取一些值。 但是,如果有人對此有任何想法,我沒有成功。 請與我分享......

到目前為止,這是我的代碼,

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