python3 dict ndarray 存成json,并保留原数据精度的实例

作者:兢兢业业小码农 时间:2021-03-04 13:25:31 

如下所示:


import numpy as np
import codecs, json

a = np.arange(10).reshape(2,5) # a 2 by 5 array
b = a.tolist() # nested lists with same data, indices
file_path = "/path.json" ## your path variable
json.dump(b, codecs.open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4) ### this saves the array in .json format

关键是tolist和codecs编码,并转成适应json的格式。

解码并还原:


obj_text = codecs.open(file_path, 'r', encoding='utf-8').read()
b_new = json.loads(obj_text)
a_new = np.array(b_new)

转自:https://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable

来源:https://blog.csdn.net/qq_28660035/article/details/82251312

标签:python3,dict,ndarray,json,数据精度
0
投稿

猜你喜欢

  • oracle学习笔记(二)

    2012-01-05 18:59:20
  • 兼容IE和FF的收藏本站、设为首页代码

    2009-01-07 14:14:00
  • Linux下安装Memcached服务器和客户端与PHP使用示例

    2023-10-05 04:32:44
  • ASP用户登录模块的设计

    2008-11-21 16:55:00
  • python单线程文件传输的实例(C/S)

    2023-04-07 22:45:48
  • 微软建议的ASP性能优化28条守则(1)

    2008-02-22 16:54:00
  • Python 高效编程技巧分享

    2022-12-13 10:23:43
  • Python更换pip源方法过程解析

    2022-01-20 08:00:58
  • python字典DICT类型合并详解

    2023-01-03 07:37:12
  • ASP实现类似Java中的Linked HashMap类

    2010-04-03 20:49:00
  • python3字符串输出常见面试题总结

    2021-01-13 08:39:23
  • 清除浮动的最简写法

    2009-03-30 15:58:00
  • 在任意字符集下正常显示网页的方法二(续)

    2023-11-22 17:36:14
  • Laravel框架文件上传功能实现方法示例

    2023-11-15 19:16:31
  • python 正则表达式 概述及常用字符

    2023-03-03 07:16:15
  • 有关asp的系统变量ServerVariables (“HTTP_USER_AGENT“)?

    2009-10-29 12:12:00
  • Python实现图像增强

    2022-07-08 10:50:50
  • python爬取亚马逊书籍信息代码分享

    2021-03-25 09:38:14
  • python socket网络编程步骤详解(socket套接字使用)

    2022-09-15 11:35:08
  • MySQL数据库配置技巧

    2009-03-06 14:32:00
  • asp之家 网络编程 m.aspxhome.com