我是靠谱客的博主 爱听歌西装,这篇文章主要介绍json文件修改-解决:TypeError: Object of type Series is not JSON serializable,现在分享给大家,希望可以做个参考。

问题描述

在导入Python json包,调用json.dump/dumps函数时,可能会遇到TypeError: Object of type Series is not JSON serializable错误,也就是无法序列化某些对象格式。

**

解决办法

**
自定义序列化方法

import time
import numpy as np
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()

然后在调用json.dump/dumps时,指定使用自定义序列化方法

file_out=open('test5.geojson', "w")
file_out.write(json.dumps(dic, cls=MyEncoder))

最后

以上就是爱听歌西装最近收集整理的关于json文件修改-解决:TypeError: Object of type Series is not JSON serializable的全部内容,更多相关json文件修改-解决:TypeError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部