#pickle保存与读取
def save_pickle_data(save_data_dir, save_data, dataname):
# 保存数据集
with open(save_data_dir + "/" + dataname + ".pickle", "wb") as handle:
# pickle.dump(save_data, handle)
pickle.dump(save_data, handle, protocol=pickle.HIGHEST_PROTOCOL)
handle.close()
def read_pickle_data(save_data_dir, dataname):
with open(save_data_dir + "/" + dataname + ".pickle", 'rb') as handle:
read_data = pickle.load(handle)
handle.close()
return read_data
#JSON格式数据
def read_json_data(save_data_dir, dataname):
with open(save_data_dir + "/" + dataname + ".json", 'rb') as handle:
read_data = json.load(handle)
handle.close()
return read_data
def save_json_data(save_data_dir, json_dict, dataname):
#result = json.dumps(json_dict, indent=4)
#print(result)
with open(save_data_dir + "/" + dataname + ".json", 'w') as handle:
json.dump(json_dict, handle, indent=4)
handle.close()
def save_json_data_ensure_ascii(save_data_dir, json_dict, dataname):
# result = json.dumps(json_dict, indent=4)
# print(result)
with open(save_data_dir + "/" + dataname + ".json", 'w',encoding="utf-8") as handle:
json.dump(json_dict, handle, indent=4,ensure_ascii=False)
handle.close()
#按行读取
def read_json_data(save_data_dir):
read_data = []
with open(save_data_dir, 'r') as f:
# read_data = json.load(handle, cls=MyEncoder)
for jsonobj in f:
try:
dataDic = json.loads(jsonobj)
read_data.append(dataDic)
except:
print("#Error#", str(jsonobj))
return read_data