欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例

 更新時(shí)間:2019年12月06日 08:38:38   作者:兢兢業(yè)業(yè)小碼農(nóng)  
今天小編就為大家分享一篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

如下所示:

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

關(guān)鍵是tolist和codecs編碼,并轉(zhuǎn)成適應(yīng)json的格式。

解碼并還原:

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

轉(zhuǎn)自:https://stackoverflow.com/questions/26646362/numpy-array-is-not-json-serializable

以上這篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論