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

时间:2018-04-08 18:35:25

标签: python django python-3.5 ebay-api

我正在使用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对象:

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"
        }
    }

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

您收到错误,因为使用x[key] = val语法只能运行一个级别。尝试分配x[y][key] = val会导致错误,因为字典中不存在y。如果您执行myitem['Item']['ItemSpecifics']={},那么在尝试在其中创建字典之前,它将起作用。或者,您可以在创建dict时立即指定它:

myitem = {
    "Item": {
        ...
        "ItemSpecifics": {}
    }
}