python3 dict ndarray 存成json,并保留原数据精度的实例
如下所示:
importnumpyasnp importcodecs,json a=np.arange(10).reshape(2,5)#a2by5array b=a.tolist()#nestedlistswithsamedata,indices file_path="/path.json"##yourpathvariable json.dump(b,codecs.open(file_path,'w',encoding='utf-8'),separators=(',',':'),sort_keys=True,indent=4)###thissavesthearrayin.jsonformat
关键是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
以上这篇python3dictndarray存成json,并保留原数据精度的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。