list的保存
def savedata(data,filename):
numpy_array = np.array(data)
np.save(filename, numpy_array)
def readNp(filename):
numpy_array = np.load(filename) #numpy_array type, not list
return numpy_array
dict的保存
import json
import datetime
import numpy as np
class JsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, datetime):
return obj.__str__()
else:
return super(MyEncoder, self).default(obj)
def save_dict(filename, dic):
'''save dict into json file'''
with open(filename,'w') as json_file:
json.dump(dic, json_file, ensure_ascii=False, cls=JsonEncoder)
网友评论