【发布时间】:2018-08-15 10:43:16
【问题描述】:
我有一个 for 循环将订单从请求返回到在线商店。然后将所需的元素插入到字典中。我的问题是,当我在一个订单中有多个项目时,它会用请求中的最后一个订单覆盖字典。
我可以使用代码中的print("this is an order") 看到订单上有多个商品。
如何增加 dict 中的索引,以便将订单项附加到它而不是覆盖它。
提前致谢。
代码:
#get specifics on each order with orderID from previous request
tags2 = ('{http://publicapi.ekmpowershop.com/}ProductCode', '{http://publicapi.ekmpowershop.com/}ProductQuantity',
'{http://publicapi.ekmpowershop.com/}ProductName', '{http://publicapi.ekmpowershop.com/}OrderDate',
'{http://publicapi.ekmpowershop.com/}ProductPrice')
for orderItem in xmlFormatter(ekmSingleOrderRequest(str(list(out.values())[0])), "C:/Users/user/Desktop/test2.xml").iter('{http://publicapi.ekmpowershop.com/}OrderItem'):
out2 = {}
#here we can the quantity of order items
print("this is an order item")
for child in orderItem:
count = 0
if child.tag in tags2:
out2[child.tag[child.tag.index('}')+1:]] = child.text
【问题讨论】:
-
将订单放入列表中。
标签: python xml python-3.x python-requests lxml