簡體   English   中英

將XML數據解析為dict,不確定最恐怖的方式

[英]Parsing XML data into a dict, not sure of the most Pythonic way to do it

假設我在具有多個價格的在線產品上有一些XML數據:

<Response>
    <TotalOffers>6</TotalOffers>
    <LowPrices>
        <LowPrice condition="new">
            <CurrencyCode>USD</CurrencyCode>
            <Amount>15.50</Amount>
        </LowPrice>
        <LowPrice condition="used">
            <CurrencyCode>USD</CurrencyCode>
            <Amount>22.86</Amount>
        </LowPrice>
    </LowPrices>
</Response>

我的最終目標是將它傳遞給一個函數,該函數將XML解析為簡化字典的形式,如下所示:

response = {
    'total_offers': 6,
    'low_prices': [
        {'condition': "new", 'currency': "USD", 'amount': 15.50},
        {'condition': "used", 'currency': "USD", 'amount': 22.86},
    ]
}

使用lxml庫這很簡單。 我只需要指定xpath來查找每個值,然后處理缺少預期數據的異常,例如獲取TotalOffers值(6)我會這樣做:

# convert xml to etree object
tree_obj = etree.fromstring(xml_text)
# use xpath to find values that I want in this tree object
matched_els = tree_obj.xpath('//TotalOffers')
# xpath matches are returned as a list
# since there could be more than one match grab only the first one
first_match_el = matched_els[0]
# extract the text and print to console
print first_match_el.text
# >>> '6'

現在我的想法是我可以編寫一個像get_text(tree_obj, xpath_to_value)這樣的函數,但是如果我還希望這個函數將值轉換為適當的類型(例如:string,float或int),我應該有一個指定的param類似於get_text(tree_obj, xpath_to_value, type='float')

因為如果我這樣做,我創建dict的下一步將是這樣的:

low_prices = []
low_prices_els = tree_obj.xpath('//LowPrices')
for el in low_prices_els:
    low_prices.append(
        {
            'condition': get_text(el, './@condition', type='str'),
            'currency': get_text(el, './CurrencyCode', type='str'),
            'amount': get_text(el, './Amount', type='float')
        }
    )

response = {
    'total_offers': get_text(tree_obj, '//TotalOffers', type='int'),
    'low_prices': low_prices
}

這是完成我想要做的最好的方法嗎? 我覺得我將來會為自己制造麻煩。

我認為你需要的工具是xml到json工具,它將xml文件轉換為json格式,你可以在下面測試它:

http://codebeautify.org/xmltojson

在此輸入圖像描述

出:

{"Response":{"TotalOffers":"6","LowPrices":{"LowPrice":[{"CurrencyCode":"USD","Amount":"15.50","_condition":"new"},{"CurrencyCode":"USD","Amount":"22.86","_condition":"used"}]}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM