use pickle module in python3

2019-11-26 15:37:37 浏览数 (1)

本文由腾讯云 社区自动同步,原文地址 http://blogtest.stackoverflow.club/use-pickle-in-python/

store data

代码语言:txt复制
import pickle
import os 
log_save_path = './log/'
log_name = "acc"
if  not os.path.exists(log_save_path):
    os.makedirs(log_save_path)
with open(log_save_path   log_name, 'wb') as f:
    pickle.dump(acc, f)

read data

代码语言:txt复制
import pickle
import os
log_save_path = './log/'
log_name = "acc"

# read a dict and put it into acc
acc = pickle.load(open('2018-04-23-13-35acc','rb'))

# transfer dict into items, it looks like (key1, value1), (key2, value2)
items = list(acc.items())

# form keys and values
# sometimes we need a vector of keys and values

keys, values = tuple(zip(*items))

0 人点赞