我正在嘗試決議專案并將其放在同一級別的產品中,如果專案有多個值,我將獲得多個具有相同 item_id 等的產品。
data = event['products']['item']
item = []
while number < len(data)
item.append(data[number])
number = number 1
uj5u.com熱心網友回復:
您需要event['products']在回圈中每次復制一個副本,然后合并data[number]到其中。
products = event['products']
result = []
for item in products['item']:
new_prod = products.copy()
del new_prod['item']
new_prod.update(item)
result.append(new_prod)
uj5u.com熱心網友回復:
方法之一;
data = {
"products": [
{
"created_time": "1234567890",
"updated_time": "1234567890",
"item": [
{
"Status": "active",
"quantity": 59,
"Images": []
},
{
"Status": "passive",
"quantity": 60,
"Images": []
}
],
"item_id": 123,
"primary_category": 345,
"marketImages": [],
"attributes": {
"name": "Sample"
}
}
]
}
output = {}
output['products'] = []
for product in data['products']:
for item in product['item']:
product1 = product.copy()
del product1['item']
product1.update(item)
output['products'].append(product1)
print (output)
輸出:
{'products': [{'created_time': '1234567890', 'updated_time': '1234567890', 'item_id': 123, 'primary_category': 345, 'marketImages': [], 'attributes': {'name': 'Sample'}, 'Status': 'active', 'quantity': 59, 'Images': []}, {'created_time': '1234567890', 'updated_time': '1234567890', 'item_id': 123, 'primary_category': 345, 'marketImages': [], 'attributes': {'name': 'Sample'}, 'Status': 'passive', 'quantity': 60, 'Images': []}]}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359687.html
上一篇:如何從JSON檔案構建模型
