python读写pkl文件

1、读取pkl文件

  方法1: 使用pickle文件

import pickle
plan = pickle.load(open("./nnUNetPlansv2.1_plans_3D.pkl"))

  如果加载中出现报错:  

   UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

  需要修改为:

import pickle
plan = pickle.load(open("./nnUNetPlansv2.1_plans_3D.pkl", "rb"))

 方法2:使用 from batchgenerators.utilities.file_and_folder_operations import *

from batchgenerators.utilities.file_and_folder_operations import *

plans_files = "./nnUNetPlansv2.1_plans_3D.pkl"
plan = load_pickle(plans_file)

 

2、读取key值

plan.keys()

 

3、修改并保存

# 如我们要修改bs,找到bs对应的key值
plan["plans_per_stage"][0]["batch_size"]=4
pickle.dump(plan, open("./nnUNetPlansv2.1_plans_3D_new.pkl", "wb"))  # 此处wb对应读时候的rb

 

你可能感兴趣的:(python,python)