1.将变量序列化并保存到文件
1 2 3 4 5 6 7 |
import pickle data = {'a': 1, 'bb': 2, 'cc': 3} with open('tmp.pk', 'wb') as f: pickle.dump(data, f) |
2.从文件中读取二进制序列并赋值给变量
1 2 3 4 5 6 7 8 |
import pickle with open('tmp.pk', 'rb') as f: data = pickle.load(f, encoding='bytes') print(data) '''结果为 {'a': 1, 'bb': 2, 'cc': 3} ''' |