b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x19\x00\x00\x00The Count of Monte Cristoq\x02X\x04\x00\x00\x00typeq\x03X\x05\x00\x00\x00Movieq\x04X\x04\x00\x00\x00funcq\x05c__main__\ntest\nq\x06u.'
<class'bytes'>
1
2
3
4
5
6
7
8
9
importpickle
get_data =pickle.loads(data)
get_data['func']('cat')
print(get_data)
#输出
hello,cat
{'name': 'The Count of Monte Cristo', 'type': 'Movie', 'func': <function test at 0x00000235350A7F28>}
2. dump && load
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
importpickle
deftest(name):
print("hello,{}".format(name))
info ={
'name': 'The Count of Monte Cristo',
'type': 'Movie',
'func': test
}
with open('test.txt', 'wb') as f:
pickle.dump(info, f)
# 写入test.txt文件中的内容
?}q (X typeqX MovieqX funcqc__main__
test
qX nameqX The Count of Monte Cristoqu.
1
2
3
4
5
6
7
8
9
importpickle
with open('test.txt', 'rb') as f:
get_data =pickle.load(f)
print(get_data)
# 输出
{'name': 'The Count of Monte Cristo', 'func': <function test at 0x000001BA2AB4D510>, 'type': 'Movie'}