Python for循环覆盖字典
Python For loop Overwriting dict
我有一个 for 循环 return 从请求到在线商店的订单。然后将所需的元素插入到字典中。我的问题是,当我在一个订单中有多个项目时,它会用请求中的最后一个订单覆盖字典。
我可以使用代码中的 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
使用列表,保存每一项:
items = []
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:
if child.tag in tags2:
out2[child.tag.split('}',1)[1]] = child.text
items.append(out2)
使用字典重要吗?您可以改用 pandas 数据框吗?
import pandas as pd
df = pd.DataFrame(["A","B"])
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")
tobeappended = {}
for child in orderItem:
count = 0
if child.tag in tags2:
tobeappended["A"] = child.tag[child.tag.index('}')+1:]
tobeappended["B"] = child.text
df = df.append(tobeappended,ignore_index=True)
我有一个 for 循环 return 从请求到在线商店的订单。然后将所需的元素插入到字典中。我的问题是,当我在一个订单中有多个项目时,它会用请求中的最后一个订单覆盖字典。
我可以使用代码中的 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
使用列表,保存每一项:
items = []
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:
if child.tag in tags2:
out2[child.tag.split('}',1)[1]] = child.text
items.append(out2)
使用字典重要吗?您可以改用 pandas 数据框吗?
import pandas as pd
df = pd.DataFrame(["A","B"])
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")
tobeappended = {}
for child in orderItem:
count = 0
if child.tag in tags2:
tobeappended["A"] = child.tag[child.tag.index('}')+1:]
tobeappended["B"] = child.text
df = df.append(tobeappended,ignore_index=True)