I have this object:
"products":[
{
"title":"ASUS ZenFone 4 Max",
"author":"1",
"date":"2017-12-17 21:49:30",
"date_gmt":"0000-00-00 00:00:00",
"content":"",
"status":"draft",
"comment_status":"open",
"image":"",
"gallery":[
],
"meta":{
"post_views_count":"0",
"aps-product-price":"0",
"aps-product-sku":"SKU",
"aps-product-stock":"InStock",
"aps-product-qty":"0",
"aps-product-on-sale":"no",
"aps-product-sale-price":"0",
"aps-product-sale-start":"",
"aps-product-sale-end":"",
"aps-product-features":[
{
"name":"CPU",
"icon":"cpu",
"value":"CPU Feature"
},
{
"name":"RAM",
"icon":"ram",
"value":"RAM Feature"
},
{
"name":"Storage",
"icon":"hdd",
"value":"Storage Feature"
},
{
"name":"Display",
"icon":"display",
"value":"Display Feature"
},
{
"name":"Camera",
"icon":"camera",
"value":"Camera Feature"
},
{
"name":"OS",
"icon":"cog",
"value":"OS Feature"
}
],
"aps-product-videos":[
],
"aps-attr-group-684":{
"618":"General-Model-Attribute",
"556":"01-12-2017",
"644":"02-12-2017",
"659":"Available"
},
"aps-attr-group-685":{
"578":"Design-Dimensions-Attribute",
"678":"Design-Weight-Attribute",
"572":"*Design-Colors-Attribute\r\n*Design-Colors-Attribute\r\n*Design-Colors-Attribute"
},
"aps-attr-group-687":{
"655":"Display-Dimensions-Attribute",
"861":"Display-Technology-Attribute",
"581":"Display-Protection-Attribute",
"645":"Display-Resolution-Attribute"
},
"aps-attr-group-690":{
"623":"Software-Operating System-Attribute",
"668":"Software-User Interface-Attribute"
},
"aps-attr-group-689":{
"631":"Camera-Front-Attribute",
"590":"Camera-Front-Camera-Features-Attribute",
"647":"Camera-Rear-Camera-Attribute",
"863":"Camera-Rear-Camera-Features-Attribute"
},
"aps-attr-group-691":{
"571":"Hardware-Chipset-Attribute",
"573":"Hardware-CPU-Attribute",
"596":"Hardware-GPU-Attribute",
"639":"512 MB",
"612":"Hardware-InternalStorage-Attribute",
"570":"Hardware-CardSlot-Attribute",
"651":"Hardware-Sensors-Attribute"
},
"aps-attr-group-686":{
"553":"Network-Networks-Attribute",
"654":"Network-SIM-Attribute"
},
"aps-attr-group-692":{
"564":"Connectivity-Bluetooth-Attribute",
"679":"Connectivity-WiFi-Attribute",
"680":"No",
"666":"Connectivity-USB-Attribute",
"595":"Connectivity-GPS-Attribute",
"621":"No"
},
"aps-attr-group-688":{
"558":"Media-AudioPlayback-Attribute",
"672":"Media-Video-Playback-Attribute",
"671":"Media-Video-Out-Attribute",
"593":"Media-FMRADIO-Attribute",
"555":"Media-Alert-Types-Attribute",
"646":"Media-Ring-Tones-Attribute",
"615":"Media-Loudspeaker-Attribute",
"598":"Media-Handsfree-Attribute"
},
"aps-attr-group-695":{
"569":"Battery-Capacity-Attribute",
"629":"Removable",
"862":"Battery-Charging-Attribute",
"658":"Battery-Standby-Attribute",
"662":"Battery-TalkTime-Attribute",
"620":"Battery-MusicPlay-Attribute"
},
"aps-product-offers":[
],
"aps-product-rating":{
"design":5,
"display":6,
"camera":5,
"performance":5,
"battery":6
},
"aps-product-rating-total":"5.4",
"aps-custom-tabs":[
],
"_disable_fbc":"",
"_yoast_wpseo_primary_aps-cats":"",
"_yoast_wpseo_content_score":"30"
},
"terms":{
"aps-cats":[
"smartphones"
]
}
},...]
and I need to achieve this dynamically in a loop. Ex:
#Initial declaration
products = {"products":{}}
#Add items in the products object
products["title"] = "ASUS ZenFone 4 Max"
products["author"] = "1"
for items in product_list:
products["meta"]["post_views_count"] = 0
products["meta"]["aps-product-sku"] = "SKU"
I can't seem to figure out how to add the next product which also has "title", "author", "meta" (this is also an object that has key-value pair as well) etc.
I hope you can understand my query. I'm currently doing web scraping :)
Thank you
Try like this:
products = []
product1 = {}
product1["title"] = "ASUS ZenFone 4 Max"
product2 = {}
product2["title"] = "Galaxy S8"
products.append(product1)
products.append(product2)
print(products)
if __name__ == '__main__':
new_list = list()
new_product_dict = {
"title":"new title",
"author":"new author",
"date":"2017-12-18 21:49:30",
"date_gmt":"0000-00-00 00:00:00",
"content":"",
"status":"draft",
"comment_status":"open",
"image":""}
for items in product_list.values():
new_list = [new_product_dict]
items.append(new_list)
print(items)
this approach works for me, of course you can add more dicts in the for loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.