将列表对象转换为字典

2024-04-27 00:14:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份清单,上面有下列项目:

poly = ['AddTicketResponse.xsd',     {
    'OrderServiceType': 'stPickUp',
    'Taxes': {'Tax': {'Caption2': None, 'Caption': 'Sales Tax IN',
              'Amount': '1.26'}},
    'CreditSubtotal': '0',
    'Version': '1',
    'NetSubtotal': '13.97',
    'CreatedTime': '2018-08-14T19:19:13Z',
    'PriceGroupName': '$$Regular Price Group$$',
    'PurchaseSubtotal': '13.97',
    'Status': 'tsOpen',
    'ItemCount': '2',
    'Seq': '105651',
    'Warnings': {'Warning': {'Message': 'The customer name from SpeedLine Connect differs from the name in the customer record.'
                 }},
    'PaymentTotal': '0',
    'Agent': None,
    'ID': '1678'
    'TaxSubtotal': '1.26',
    'MenuSelections': {'MenuSelection': [{
        'MenuCalculatedPriceOfAggregatedSelections': '0',
        'PLU': '44'
        }]}
    }]

这里有两个对象: 我是保利[1],这是:

{
    'OrderServiceType': 'stPickUp',
    'Taxes': {'Tax': {'Caption2': None, 'Caption': 'Sales Tax IN',
              'Amount': '1.26'}},
    'CreditSubtotal': '0',
    'Version': '1',
    'NetSubtotal': '13.97',
    'CreatedTime': '2018-08-14T19:19:13Z',
    'PriceGroupName': '$$Regular Price Group$$',
    'PurchaseSubtotal': '13.97',
    'Status': 'tsOpen',
    'ItemCount': '2',
    'Seq': '105651',
    'Warnings': {'Warning': {'Message': 'The customer name from SpeedLine Connect differs from the name in the customer record.'
                 }},
    'PaymentTotal': '0',
    'Agent': None,
    'ID': '1678'
    'TaxSubtotal': '1.26',
    'MenuSelections': {'MenuSelection': [{
        'MenuCalculatedPriceOfAggregatedSelections': '0',
        'PLU': '44'
        }]}
    }

作为一个单独的字典,这样我就可以得到“MenuSelections”的所有值 到目前为止,我只能将xml文件转换为列表并得到这些值。你知道吗

root = ElementTree.XML(pos_xml)
xmldict = XmlDictConfig(root)
poly=[]
for key, value in xmldict.iteritems():
    poly.append(value)

老实说,任何建议都会有帮助。你知道吗


Tags: thenameinfromnonecustomertaxsales
1条回答
网友
1楼 · 发布于 2024-04-27 00:14:22
d = poly[1]

它将在poly列表的第二个位置提取对字典的引用,并将其存储在新名称d。你知道吗

你可以用它来得到你想要的

print(d['MenuSelections']['MenuSelection'])

您也可以直接索引poly[1]

print(poly[1]['MenuSelections']['MenuSelection'][0]['PLU'])

将打印

44

相关问题 更多 >