一. 对象持久化
1. 扁平文件
- 文本文件
scores = [88,99,77,55]
def write_scores():
with open('data.list.txt', 'w', encoding='utf8') as f:
f.write(str(scores))
print('complete')
def read_scores():
with open('data.list.txt', 'r', encoding='utf8') as f:
lst = eval(f.read()) #字符串转化成python表达式
lst[0] = 99
print(lst)
if __name__ == '__main__':
read_scores()
2.pickle
- pickle
(1)序列化为字符串
import pickle
person = {
'name':'tom','age':20