在python中获取自定义对象的keyvalue对

2024-05-28 11:16:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在与python boto package called mws合作,与Amazon MWS进行接口。但我觉得这也是一个一般的python问题,还是不确定。我使用list_oder_items方法获取订单的产品项。在

items = conn.list_order_items(AmazonOrderId = order_id)
result = items.ListOrderItemsResult

然后我要保存每个项目。在

^{pr2}$

这里的item变量是一个<class 'boto.mws.response.OrderItem'>类型,如果打印出来,会显示如下:

OrderItem{}(Title: u'title', GiftWrapPrice: USD 0.00, ConditionNote: u'Brand New& Authentic product....', CODFee: None, ASIN: u'XXXX', OrderItemId: u'XXXX', CODFeeDiscount: None, QuantityShipped: u'1', GiftWrapTax: USD 0.00, ShippingPrice: USD 3.99, QuantityOrdered: u'1', ItemTax: USD 0.00, PromotionIds: [], SellerSKU: u'XXXX', ShippingDiscount: USD 0.00, ShippingTax: USD 0.00, ConditionId: u'New', PromotionDiscount: USD 0.00, ItemPrice: USD 12.81, ConditionSubtypeId: u'New')

为什么我不能像for key in item: print key那样迭代键值对?只是有点傻,虽然我也试过这个:for key in dict(item): print key。也没有打印任何东西。在

我只想有一个更系统的方法来处理item对象,而不是目视检查。有没有可能把它变成一个普通的python dict?在


Tags: 方法keyinnonenewfororderitems
1条回答
网友
1楼 · 发布于 2024-05-28 11:16:21

基本上你得到了一个对象,你想在这个对象的所有字段上迭代key:值对。在

您可以尝试object._dict_来获取对象的字典表示。在

代码示例:

for key in item.__dict__:
    print key

相关问题 更多 >