Python基础知识(十)

扁平文件、pickle、shelve、数据库、ORM

eval :将字符串转化为列表
scores=[99,88,77,55]
def write_scores():
with open(‘data_list.txt’,‘w’,encoding=‘utf8’) as f:
f.write(str(scores))
print(‘文件写入完成…’)
def read_scores():
with open(‘data_list.txt’,‘w’,encoding=‘utf8’) as f:
list=eval(f.read())
list[0]=99
print(list)
if name==‘main’:
write_scores()

pickle 应用

对象序列化反序列化
pickle.dumps:将对象序列为字符串
pickle.loads:将字符串还原为序列
pickle.dump(obj,file):
pickle.load(file):
Python基础知识(十)

shelve 应用

Python基础知识(十)
.open(‘dbfile’)
db[‘key’=obj
len(db)
del db【‘key’】

相关文章:

  • 2021-12-12
猜你喜欢
  • 2021-12-27
  • 2021-08-13
  • 2021-09-03
  • 2021-05-21
  • 2021-08-15
  • 2021-07-13
  • 2021-12-02
相关资源
相似解决方案