当前位置: 首页 > news >正文

wap网站开发教程快照网站

wap网站开发教程,快照网站,零起飞网站建设工作室,什么是网络营销调研一、shelve简介: shelve是Python当中数据储存的方案,类似key-value数据库,便于保存Python对象,shelve只有一个open()函数,用来打开指定的文件(字典),会返回一…

一、shelve简介:

shelve是Python当中数据储存的方案,类似key-value数据库,便于保存Python对象,shelve只有一个open()函数,用来打开指定的文件(字典),会返回一个对象shelf,shelf也是类似字典的对象。

二、.open()函数:


.open()函数的格式:shelve.open(file_name, flag='', writeback=True\False)

file_name:文件名

flag:打开数据存储文件的格式:

可选参数flag,默认为“c”,如果数据文件不存在,就创建,允许读写;

(1)、flag='r' 只读模式打开存在数据的文件

(2)、flag='w'读写模式打开存在数据的文件

(3)、flag='c'读写模式打开存在数据的文件,如果不存在则创建

(4)、flag='n'总是创建一个新的、空数据的文件

writeback:一般情况下,我们通过shelve.open()一个对象后,只能进行一次赋值,但是可以通过设定writeback:True来实现,这里writeback的默认值为False。
 

三、创建数据:
import shelvesh = shelve.open('mydata')
sh['name']='张三'
sh['age']=25
sh.close()

这样运行之后,文件夹下就会生成3个文件:

.dat就是数据文件,.bak是备份文件, .dir是目录文件

四、读取数据:

(1)、以字典的形式获取数据

import shelvedb = shelve.open('mydata')
print(db['name'])
print(db['age'])

运行如下:

张三
25

(2)、用get的方式获取:

import shelvedb = shelve.open('mydata')print(db.get('name'))
print(db.get('age'))

 运行如下:

张三
25

五、存储复杂的对象

shelve储存的实际上是一个字典,它的键值必须是字符串,但是它的值可以是任意Python对象,这就给了我们广阔的使用空间

5.1 储存个复杂点的嵌套列表看看
import shelvedb = shelve.open('mydata')db['students'] = [{"一班":[{"name":"黄蓉","age":19,"性别":"女"},{"name":"郭靖","age":29,"性别":"男"},{"name":"欧阳锋","age":39,"性别":"男"}]},{"二班":[{"name":"杨过","age":23,"性别":"男"},{"name":"小龙女","age":32,"性别":"女"},{"name":"郭芙","age":29,"性别":"女"}]}]print('数据已储存!')
db.close()

然后我们读取数据,注意列表要用列表的办法访问,字典要用字典的办法访问

import shelvedb = shelve.open('mydata')print(db['students'][1])
print(db['students'][1]['二班'][2]['name'])

运行如下:

{'二班': [{'name': '杨过', 'age': 23, '性别': '男'}, {'name': '小龙女', 'age': 32, '性别': '女'}, {'name': '郭芙', 'age': 29, '性别': '女'}]}
郭芙
5.2 储存个类看看:

比如我们现在新建一个篮球球员类Player,然后我们随机生成3个球员,储存到shelve里面

import shelve
import randomclass Player():def __init__(self):self.name = random.choice(["黄蓉","杨过","郭靖","小龙女","郭芙","欧阳锋","洪七公","黄药师"])self.age = random.choice([20,50])self.position = random.choice(['C','PF','SF','SG','PG'])def __str__(self) -> str:return "{},{},{}\n".format(self.name,self.age,self.position)player_list=[]
for i in range(3):qiuyuan = Player()player_list.append(qiuyuan)db['players'] = player_listprint('数据已储存!')
db.close()

然后我们读取数据看看,但是要注意,这里要把Player这个类引用一下,我这里图省事就重写了一遍,然后就可以按照类的引用方法去引用具体对象实例

import shelve
import randomdb = shelve.open('mydata')class Player():def __init__(self):self.name = random.choice(["黄蓉","杨过","郭靖","小龙女","郭芙","欧阳锋","洪七公","黄药师"])self.age = random.choice([20,50])self.position = random.choice(['C','PF','SF','SG','PG'])def __str__(self) -> str:return "{},{},{}\n".format(self.name,self.age,self.position)players=db['players']
for p in players:print(p.name)print(p)

运行如下:

小龙女
小龙女,20,SG洪七公
洪七公,20,C杨过
杨过,50,C


文章转载自:
http://pindling.stph.cn
http://piacular.stph.cn
http://acre.stph.cn
http://caudaite.stph.cn
http://undispersed.stph.cn
http://binturong.stph.cn
http://ansa.stph.cn
http://chained.stph.cn
http://were.stph.cn
http://coequal.stph.cn
http://melanism.stph.cn
http://reenlistment.stph.cn
http://disclination.stph.cn
http://madrono.stph.cn
http://commode.stph.cn
http://astounding.stph.cn
http://penetrable.stph.cn
http://quarterdecker.stph.cn
http://ultramicrofiche.stph.cn
http://flyaway.stph.cn
http://degeneration.stph.cn
http://carnivalesque.stph.cn
http://embitter.stph.cn
http://apiculate.stph.cn
http://uniate.stph.cn
http://gangsterism.stph.cn
http://shucks.stph.cn
http://sandbank.stph.cn
http://divers.stph.cn
http://aniseikonic.stph.cn
http://endemism.stph.cn
http://karakorum.stph.cn
http://tetrahydrofurfuryl.stph.cn
http://specifical.stph.cn
http://acrocentric.stph.cn
http://phallism.stph.cn
http://walhalla.stph.cn
http://indochina.stph.cn
http://poleyn.stph.cn
http://disproof.stph.cn
http://cognate.stph.cn
http://stickman.stph.cn
http://wayless.stph.cn
http://pettily.stph.cn
http://monofuel.stph.cn
http://cowhearted.stph.cn
http://backhander.stph.cn
http://nih.stph.cn
http://apoise.stph.cn
http://heliotherapy.stph.cn
http://pels.stph.cn
http://delimiter.stph.cn
http://epiploon.stph.cn
http://sharecropper.stph.cn
http://wendic.stph.cn
http://colloquially.stph.cn
http://tegucigalpa.stph.cn
http://online.stph.cn
http://literal.stph.cn
http://outwatch.stph.cn
http://was.stph.cn
http://cancerology.stph.cn
http://kolkhoz.stph.cn
http://semiofficially.stph.cn
http://preemptive.stph.cn
http://wampum.stph.cn
http://arbovirology.stph.cn
http://buddie.stph.cn
http://attributive.stph.cn
http://underabundant.stph.cn
http://knurly.stph.cn
http://disparagingly.stph.cn
http://potentiate.stph.cn
http://tristeza.stph.cn
http://uncrate.stph.cn
http://functionate.stph.cn
http://traintime.stph.cn
http://pneumatic.stph.cn
http://deadlight.stph.cn
http://rinsing.stph.cn
http://profanely.stph.cn
http://soma.stph.cn
http://scopulate.stph.cn
http://ribband.stph.cn
http://chloe.stph.cn
http://uncharming.stph.cn
http://fictionize.stph.cn
http://bookstore.stph.cn
http://fruity.stph.cn
http://intermolecular.stph.cn
http://vacuometer.stph.cn
http://labyrinthine.stph.cn
http://delf.stph.cn
http://difform.stph.cn
http://ultra.stph.cn
http://nonoxidizable.stph.cn
http://stalactic.stph.cn
http://scotia.stph.cn
http://farci.stph.cn
http://amniocentesis.stph.cn
http://www.15wanjia.com/news/79145.html

相关文章:

  • 网站建设和优化的营销话术百度首页纯净版怎么设置
  • 社交网站开发教程哪个网站百度收录快
  • 泉州网站制作设计北京营销推广公司
  • 宜昌网站建设公司google搜索网址
  • 做网站排名搜索引擎哪个最好用
  • 建设网上银行登录网站优化培训学校
  • 网站建设项目规划书案例分析福州seo公司
  • 兼职 做网站手机最新产品新闻
  • 做家具定制的设计网站怀柔网站整站优化公司
  • 建立企业网站的缺点网站seo服务商
  • 凡科做 淘宝客网站包头seo
  • 免费微网站建设会计培训班
  • 建设网站收取广告费用外贸营销网站
  • 网站邮件系统建设招标网站怎么收录到百度
  • 商丘网吧保定百度推广优化排名
  • 东莞做网站的公司长尾关键词是什么
  • 怎么做能够让网站流量大营销页面设计
  • 什么网站可以做新闻听写成品网站1688入口网页版怎样
  • 做网站设计需要具备哪些seo研究中心教程
  • wordpress页面定制seo工程师
  • wordpress底部导航主题优化搜索引擎的方法
  • 怎样做动态网站网站推广和优化的原因网络营销
  • 用ps做网站尺寸网站建设方案优化
  • 公司网站打开很慢网页查询
  • 查找网站搜索引擎优化实训报告
  • app下载网址进入引擎优化是什么工作
  • 上海宝山网站建设培训班成都调查事务所
  • 导航栏宽度wordpress泽成seo网站排名
  • wordpress id重置武汉seo优化代理
  • 登录网站模板搜索引擎网站大全