python 基础之数据持久化存储
###数据持久化存储 - 说明:持久化存储方案,普通文件、数据库、序列化 - 示例: ```python import pickle class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return 'name:{} age:{}'.format(self.name, self.age) xiaoming = Person('xiaoming', 20) # print(xiaoming) # 序列化:会将对象转换为bytes # s = pickle.dumps(xiaoming) # print(s) # 反序列化:从bytes中解析出对象 # xm = pickle.loads(s) # print(xm, type(xm)) # 直接保存到文件 # fp = open('data.txt', 'wb') # pickle.dump(xiaoming, fp) # fp.close() # 从文件中读取对象 fp = open('data.txt', 'rb') xm = pickle.load(fp) print(xm, type(xm))