栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

错误描述:使用如下方法将内容以json格式写入:

def write_merged_dataset(feature_test, bert_predictions, label_test, test_data_path):
    with open(test_data_path, 'w', encoding='utf-8') as fp:
        for idx in range(len(label_test)):
            line = {}
            line['id'] = str(idx)
            for i, feature in enumerate(feature_test[idx]):
                line['x%d' % i] = feature
            line['bert_predictions'] = bert_predictions[idx]
            line['label'] = label_test[idx]
            fp.write(json.dumps(line) + 'n')
    print('data stored at ', test_data_path)
    return True

结果报错:

TypeError: Object of type 'int64' is not JSON serializable

原因是我传入的bert_predictions参数是一个numpy.array,而numpy数组存储整数时是以class ,无法被json序列化。

解决方法:转成list再处理

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号