python使用shelve保存变量
发布时间:2024-07-07 19:46:49

1、Python中的变量可以通过shelve模块保存到二进制shelf文件中。
这样,程序就可以从硬盘中恢复变量数据。
importshelve
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()以上是python使用shelve保存变量的方法,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。
