python np array转json

Posted 潜水的阳光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python np array转json相关的知识,希望对你有一定的参考价值。

 np array转json

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

json转np array

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

 

以上是关于python np array转json的主要内容,如果未能解决你的问题,请参考以下文章