python用shelve保存变量的方法
更新:HHH   时间:2023-1-7


本篇内容主要讲解“python用shelve保存变量的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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()

到此,相信大家对“python用shelve保存变量的方法”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

返回编程语言教程...