json文件不可以保存array数组。所以需要先将词典转换为列表再保存成json文件。
from collections import defaultdict, OrderedDict
import os
import json
import numpy as np
import json
file_path = r"./test_npz/"
def switch_npz_to_json(data_type:str):
os.makedirs(f'./{data_type}_npz/')
filenames = os.listdir(file_path)
all_file = {}
json_path = f'./{data_type}_npz/all_data.json'
for file in filenames:
load_path = os.path.join(file_path, file)
name = file.split('.')[0]
datas = np.load(load_path, allow_pickle = True)
key1 = 'target_sequence'
key2 = 'binding_sites'
key3 = 'binding_matrix'
#JSON文件不能保存array数组。
#value1 = np.array(datas['target_sequence'], dtype=np.int32)
#value2 = datas['binding_sites'].tolist()
#value3 = np.array(datas['binding_matrix'], dtype=np.int32)
value1 = datas['target_sequence'].tolist()
value2 = datas['binding_sites'].tolist()
value3 = datas['binding_matrix'].tolist()
all_dict = {key1:value1, key2:value2, key3:value3}
all_file[f'{name}'] = all_dict
with open(json_path, "w") as f:
json.dump(all_file, f)
switch_npz_to_json('fragment')