我在python中有一个像这样的口令:
mydic = {
'x1': list_of_numpy_array,
'x2': a numpy_array,
'x3': a list_of_numpy_array,
'x4': a list_of_numpy_array
}我想把这个字典从flask-api发送到调用这个接口的客户端。当我使用这段代码时:
class GetRepresentationResource(Resource):
url = 'representation'
def get(self):
# previous lines of my code which generates mydic
return {
'data': mydict,
'err': ''
}我得到了错误:Is not serializeable
发布于 2020-10-10 00:23:59
我抛出这个错误是因为np.array是不可序列化的。
在序列化之前,您需要将它们转换为一个列表
尝试以下操作:
for k,v in mydict.items():
if(isinstance(v, np.ndarray)):
mydict[k] = v.tolist()参见this Repl.it。
请注意,您需要确保数组中的类型是可序列化的
https://stackoverflow.com/questions/46318383
复制相似问题