Python 删减pkl内容

import os
import pickle
import numpy as np

def read_pickle(work_path):
 	'''
    todo:   read pkl to list
    '''
    data_list = []
    with open(work_path, "rb") as f:
            while True:
                try:
                    data = pickle.load(f)
                    if data['image_path'] == 'xxxx':
                        continue
                    data_list.append(data)
                except EOFError:
                    break
    return data_list

def write_pkl(save_path, image_list):
    '''
    todo:   write list to pkl
    '''
    train_pkl_path = os.path.join(save_path, 'train.pkl') # can change file name str
    with open(train_pkl_path, "wb") as train_pkl:
        for i in image_list:
            pickle.dump(i, train_pkl)
       

def main():
    pkl_path = '/home/xxx/train.pkl'
    data_list = read_pickle(pkl_path)
    write_pkl(os.path.dirname(pkl_path), data_list)

if __name__=="__main__":
    main()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值