python使用shelve保存变量

552次阅读
没有评论

python使用shelve保存变量

1、用shelve模块,可以将Python中的变量保存到二进制的shelf文件中。

这样,程序就可以从硬盘中恢复变量的数据。

import shelve
shelfFile = shelve.open('mydata')
cats = ['Zonphie','Pooka','Simon']
shelfFile['cats'] = cats
shelfFile.close()

2、shelf值不必用读模式或写模式打开,因为打开后,既能读又能写。

shelfFile = shelve.open('mydata')
type(shelfFile)
 
shelve.DbfilenameShelf
 
shelfFile['cats']
 
shelfFile.close()
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-08-04发表,共计323字。
新手QQ群:570568346,欢迎进群讨论 Python51学习