一个简单的脚本程序----Python
1.
#!/usr/bin/python3.3
import cPickle as p
#the class of people information
class People:
def __init__(self,ID,name,age,address):
self.ID=ID
self.name=name
self.age=age
self.address=address
def printName(self):
print(self.name)
def printAddress(self):
print(self.name+"---"+self.address)
#Create two people
a=People(1,"yufeng",24,"HUST")
b=People(2,"luqun",23,"XianDianzi")
#Create a dictionary for people
dict1={1:a,2:b}
#Write to the file
f=file("people_info.txt",'w')
#dump the object to a file
p.dump(dict1,f)
f.close()
f=file("people_info.txt")
dict2=p.load(f)
print(dict2[1].printAddress())
本文介绍了一个使用Python实现的简单脚本程序。该脚本通过定义一个人员信息类,并创建两个实例来演示如何使用cPickle模块进行对象的序列化和反序列化操作。具体步骤包括定义类、创建实例、将对象写入文件、从文件读取对象并调用其方法。

被折叠的 条评论
为什么被折叠?



