-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02PickleDemo.py
45 lines (34 loc) · 1.08 KB
/
02PickleDemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
使用pickle进行二进制IO(读写任意类型的Python数据,而不限于字符或字节)
'''
import pickle
from tkinter import filedialog
class Person:
def __init__(self,name,age,hobbies):
self.name = name
self.age = age
self.hobbies = hobbies
def __str__(self):
return str({"name":self.name,"age":self.age,"hobbies":self.hobbies})
def pickleDumpOutput():
global file
savePath = filedialog.asksaveasfilename()
file = open(savePath, mode="ab")
data = [1, 2, 3]
data = True
data = Person("张三丰",100,["太极","八卦","喝茶"])
pickle.dump(data, file)
file.close()
def pickleLoadInput():
global file
with open(filedialog.askopenfilename(), mode="rb") as file:
data = pickle.load(file)
print(data,type(data))
data = pickle.load(file)
print(data,type(data))
data = pickle.load(file)
print(data,type(data))
data = pickle.load(file)
print(data,type(data))
pickleDumpOutput()
#pickleLoadInput()