码迷,mamicode.com
首页 > 编程语言 > 详细

python np array转json

时间:2019-11-03 01:11:46      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:sep   load   codec   list   array   imp   nes   coding   utf-8   

 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

标签:sep   load   codec   list   array   imp   nes   coding   utf-8   

原文地址:https://www.cnblogs.com/yinliang/p/11784933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!