当前位置: 代码迷 >> python >> 将XML数据解析为dict,不确定最恐怖的方式
  详细解决方案

将XML数据解析为dict,不确定最恐怖的方式

热度:42   发布时间:2023-06-13 13:54:54.0

假设我在具有多个价格的在线产品上有一些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"}]}}}
  相关解决方案