shelve持久化有时会出错文件还大,打开也慢,今天刚上hdf5,分享下
首先pip install h5py
我把函数封装在utility.py
import os
import platform
import zlib
import pickle
import platform
import numpy as np
import h5py
if platform.uname().system == "Windows":
LINK_SIGN = "\\"
elif platform.uname().system == "Linux":
LINK_SIGN = "/"
#------------------------------------------------------------------------------------
def save_h5(filename:str,data:Any,overwrite:bool=False):
"""
1.保存hdf5数据
2.overwrite为True覆盖源文件,为False增量更新文件
"""
contract_file_path = get_folder_path(filename)
filepath =f"{contract_file_path}{LINK_SIGN}{filename}.h5"
if overwrite:
raw_data = data
else:
#增量更新数据
raw_data = load_h5(filename)
if isinstance(raw_data,dict):
raw_data.update(data)
elif isinstance(raw_data,list):
for value in data:
if value not in raw_data:
raw_data.append(value)
#循环写入h5数据直到写入成功或重试3次后退出循环
count = 0
while True:
count += 1
status = save_h5_status(filepath,raw_data)
if status or count > 3:
break
#------------------------------------------------------------------------------------
def save_h5_status(filepath:str,raw_data:Any):
"""
获取H5保存数据状态
"""
try:
with h5py.File(filepath,"w") as file:
data = zlib.compress(pickle.dumps(raw_data), 5)
file["data"] =np.void(data)
return True
except:
return False
#------------------------------------------------------------------------------------
def load_h5(filename:str):
"""
读取hdf5数据
"""
contract_file_path = get_folder_path(filename)
filepath =f"{contract_file_path}{LINK_SIGN}{filename}.h5"
if not os.path.exists(filepath):
return {}
count = 0
while True:
count += 1
status,data = load_h5_status(filepath)
if status or count > 3:
return data
#------------------------------------------------------------------------------------
def load_h5_status(filepath:str):
"""
获取H5读取状态及数据
"""
try:
with h5py.File(filepath,"r") as file:
data = file["data"][()]
data = pickle.loads(zlib.decompress(data))
return True,data
except:
return False,{}