如何删除/跳过<;类别';非类型'&燃气轮机;Python中的对象
我正在从供应商处接收来自SOAP API调用的数据,并使用Zeep库。数据是如何删除/跳过<;类别';非类型'&燃气轮机;Python中的对象,python,json,iteration,nonetype,zeep,Python,Json,Iteration,Nonetype,Zeep,我正在从供应商处接收来自SOAP API调用的数据,并使用Zeep库。数据是类“NoneType”,无法迭代。我的任务是删除/跳过非类型对象 如果我收到一个包含一些值的响应,我可以jsonify它,但是,如果响应没有返回值,我不能jsonify它或删除它 例如,我传递了两个参数列表并收到了两个响应,一个包含数据,另一个没有。 我的代码如下: # Making a SOAP call and save the response response = client.service.GetOrders
类“NoneType”
,无法迭代。我的任务是删除/跳过非类型对象
如果我收到一个包含一些值的响应,我可以jsonify它,但是,如果响应没有返回值,我不能jsonify它或删除它
例如,我传递了两个参数列表并收到了两个响应,一个包含数据,另一个没有。
我的代码如下:
# Making a SOAP call and save the response
response = client.service.GetOrders(**params[0])
# convert the response object to native Python data format
py_response = helpers.serialize_object(response)
# jsonify (list of dictionaries)
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
print(type(response_list))
print(response_list)
因此,输出如下:
<class 'list'> # successfully converted
[{'AgentID': 0, 'AgentName': 'Not specified', 'CustomerID': 1127}]
<class 'NoneType'> # was not converted
None
错误:
TypeError:“NoneType”对象不可编辑
clean\u response\u list=[x代表x,如果x!=None]
这不起作用,因为response_list为None,所以您无法对其进行迭代
尝试:
或
或
或
不过,我可能会把支票稍微调高一点<代码>打印(py_响应)查看它的外观。然后编辑你的代码,检查它是否有效,然后创建响应列表并执行这些操作。这实际上并没有回答任何问题。这只是一个if语句,我尝试了各种方法。如果我检查了py响应,它就是我答案中的输出。数据和无。是的,很明显,我需要编辑我的代码来检查数据的有效性-这正是我问的问题。您打印了响应列表,而不是py\u响应。两者的输出是相同的。
如果响应列表为无:响应列表=[]
这是最后的诀窍!我已经接受了答案!谢谢你的帮助。
clean_response_list = [x for x in response_list if x != None]
response_list = response_list or []
if response_list is None:
response_list = []
if py_response is not None:
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
else:
response_list = []
if py_response:
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
else:
response_list = []