Python ebay sdk - 如何动态添加项目细节?

Python ebay sdk - how to add item specifics dynamically?

我正在使用 ebay-python-sdk 我创建了 myitem 对象,所以我可以 api.execute('VerifyAddItem', myitem) 它。 但是我需要添加ItemSpecifics,这里我遇到了问题。 我有一组由元组组成的项目细节:

a = [('Occasion', 'Casual'), ('Brand', 'Ralph Lauren'), ('Style', 'Polo Shirt')]

我想将它添加到 'myitem'。 所以我使用:

for p in a:
    ar.append([{"Name":p[0]}, {"Value":p[1]}])

myitem["Item"]["ItemSpecifics"]["NameValueList"] = ar

但我收到错误“KeyError: 'ItemSpecifics'” 我需要在我的代码中更改什么才能正确添加它?

如果我把这段代码写到myitem对象中,就可以了。就是不知道怎么动态添加。

"ItemSpecifics": {
                "NameValueList": [
                    {"Name": "Occasion", "Value": "Casual"},
                    {"Name": "Brand", "Value": "Ralph Lauren"},
                    {"Name": "Style", "Value": "Polo Shirt"},
                    {"Name": "Sleeve Style", "Value": "Short Sleeve"}
                ]
            },

我的项目对象:

myitem = {
        "Item": {
            "Title": name,
            "Description": "<![CDATA[{}]]>".format(desc),
            "PrimaryCategory": {"CategoryID": "176984"},
            "StartPrice": str(round(Decimal(start_price), 2)),
            "CategoryMappingAllowed": "true",
            "Country": "GB",
            "ConditionID": "1000",
            "Currency": "GBP",
            "DispatchTimeMax": "3",
            "ListingDuration": "{}".format(str(auction_len)),
            "ListingType": "FixedPriceItem",
            "PaymentMethods": "PayPal",
            "PayPalEmailAddress": "test@ebay.com",
            "PictureDetails": {"PictureURL": pict_list},
            "PostalCode": "bh102as",
            "ProductListingDetails":{
               "EAN": "8054241786423",
            },
            "Quantity": str(round(Decimal(qty), 0)),
            "ReturnPolicy": {
                "ReturnsAcceptedOption": "ReturnsAccepted",
                "RefundOption": "MoneyBack",
                "ReturnsWithinOption": "Days_30",
                "Description": "If you are not satisfied, return the book for refund.",
                "ShippingCostPaidByOption": "Buyer"
            },
            "ShippingDetails": [{
                "ShippingType": "Free",
                "ShippingServiceOptions": {
                    "FreeShipping": "true",
                    "ShippingServicePriority": "1",
                    "ShippingService": "ShippingMethodStandard",
                    "ShippingServiceCost": "0"
                }
            },
            {
                "ShippingType": "Flat",
                "ShippingServiceOptions": {
                    "FreeShipping": "false",
                    "ShippingServicePriority": "2",
                    "ShippingService": "UK_RoyalMailSecondClassStandard",
                    "ShippingServiceCost": "0.50"
                }
            }],
            "Site": "UK"
        }
    }

你能给我指出正确的方向吗?

提前致谢。

你收到一个错误,因为使用 x[key] = val 语法只能深入一层。尝试分配 x[y][key] = val 会导致错误,因为字典中不存在 y。如果您执行 myitem['Item']['ItemSpecifics']={},然后尝试在其中创建字典,它将起作用。或者,您可以在创建 dict:

时立即指定它
myitem = {
    "Item": {
        ...
        "ItemSpecifics": {}
    }
}