将Python复杂数据类型解析为Python dict [英] Parsing Suds SOAP complex data type into Python dict

查看:410
本文介绍了将Python复杂数据类型解析为Python dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据来自 SOAP API使用 Suds ,我需要在我的 Python 脚本。在我开始写一个解析器之前(不仅仅是这样做):

I have some data coming from a SOAP API using Suds which I need to parse in my Python script. Before I go off and write a parser (there is more than just this one to do):

1)有谁知道这是什么? >这是标准的复杂对象数据类型,由 Suds (文档)。应该会发现。

1) Does anyone recognise what this is? It's the standard complex object datatype as returned by Suds (documentation). Should have spotted that.

2)如果是,是否有现有的库可以用于将其转换为Python字典?如何我将这个对象解析成Python字典吗?似乎我可以通过一个字典到Suds,但看不到一个简单的方法来恢复一个。

2) If so, is there an existing library that I can use to convert it to a Python dictionary? How do I parse this object into a Python dict? It seems I can pass a dictionary to Suds but can't see an easy way of getting one back out.

(ArrayOfBalance){
   Balance[] = 
      (Balance){
         Amount = 0.0
         Currency = "EUR"
      },
      (Balance){
         Amount = 0.0
         Currency = "USD"
      },
      (Balance){
         Amount = 0.0
         Currency = "GBP"
      },
 }


推荐答案

正确的答案,这个例子是在文档。 (括号中的位)是可以包含其他对象或类型的对象。

The right answer, as is often the case, is in the docs. The bits in (brackets) are objects which can contain other objects or types.

在这种情况下,我们有一个 ArrayOfBalance 对象,其中包含余额类型的列表,每个类型的属性为 Amount

In this case we have an ArrayOfBalance object which contains a list of Balance types, each of which has the attributes of Amount and Currency.

这些都可以使用

balance = {item.Currency: item.Amount for item in response.Balance}  

这篇关于将Python复杂数据类型解析为Python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆