将 Suds SOAP 复杂数据类型解析为 Python dict [英] Parsing Suds SOAP complex data type into Python dict
问题描述
我有一些数据来自使用 Suds
的 SOAP
API,我需要在我的 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"
},
}
推荐答案
suds.client.Client
类中有一个名为 dict
的类方法,它接受一个 sudsobject
作为输入并返回 Python dict
作为输出.在这里查看:官方泡沫文档
There is a class method called dict
in suds.client.Client
class which takes a sudsobject
as input and returns a Python dict
as output. Check it out here: Official Suds Documentation
结果片段变得如此优雅:
The resulting snippet becomes as elegant as this:
from suds.client import Client
# Code to obtain your suds_object here...
required_dict = Client.dict(suds_object)
您可能还想查看 items
类方法 (link) 在同一个类中,它从 suds_object 中提取项目,类似于 dict
上的 items
方法.
You might also want to check out items
class method (link) in the same class which extracts items from suds_object similar to items
method on dict
.
这篇关于将 Suds SOAP 复杂数据类型解析为 Python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!