TypeError: Object of type ‘float32‘ is not JSON serializable

简介: TypeError: Object of type ‘float32‘ is not JSON serializable

先将list转换成numpy.array,在将numpy.array转换成list

import numpy as np
import json
b=np.array(a).tolist()
json_str=json.dumps(b)

ok

示例:

def load_datum(filename):
    with open(osp.join(data_root,'pointlines',filename+'.pkl'),'rb') as handle:
        d = pickle.load(handle, encoding='latin1')
        h, w = d['img'].shape[:2]
        points = d['points']
        lines = d['lines']
        junctions = d ['junction']
        lsgs = np.array([[points[i][0], points[i][1], points[j][0], points[j][1]] for i, j in lines],
                        dtype=np.float32)
        image = d['img']
    return image, {'filename': filename+'.png', 
            'lines'   : lsgs.tolist(), 'junctions'   : np.array(junctions).tolist(), 'height':image.shape[0], 'width': image.shape[1]}
目录
相关文章
|
存储 数据采集 数据处理
Pandas中批量转换object至float的高效方法
在数据分析中,常需将Pandas DataFrame中的object类型列转换为float类型以进行数值计算。本文介绍如何使用`pd.to_numeric`函数高效转换,并处理非数字值,包括用0或平均值填充NaN值的方法。
1356 1
|
Python
通过 type 和 object 之间的关联,进一步分析类型对象
通过 type 和 object 之间的关联,进一步分析类型对象
481 3
|
Docker 容器
成功解决:Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but
这篇文章讨论了在使用Docker启动Elasticsearch容器时遇到的一个具体问题:由于配置文件`elasticsearch.yml`解析出错导致容器启动失败。文章提供了详细的排查过程,包括查看容器的日志信息、检查并修正配置文件中的错误(特别是空格问题),并最终成功重新启动了容器。
|
JSON 数据格式 Python
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
1235 1
|
JSON 前端开发 数据格式
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
788 0
|
存储 Java
百度搜索:蓝易云【Java语言之float、double内存存储方式】
由于使用IEEE 754标准进行存储,float和double类型可以表示非常大或非常小的浮点数,并且具有一定的精度。然而,由于浮点数的特性,它们在进行精确计算时可能会存在舍入误差。在编写Java程序时,需要注意使
471 0
|
存储 C语言
C 语言实例 - 计算 int, float, double 和 char 字节大小
C 语言实例 - 计算 int, float, double 和 char 字节大小。
501 1
|
存储 C语言
使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小
【10月更文挑战第13天】使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。
980 1
|
存储 C语言
计算 int, float, double 和 char 字节大小
计算 int, float, double 和 char 字节大小。
688 3
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
1478 2