Python---shelve模块

shelve模块
优点:
shelve模块在将Python数据持久化到本地磁盘时允许我们可以像操作dict一样操作被序列化的数据,而不必一次性的保存或读取所有数据,且读的顺序与写入的顺序相同。
序列化:
db.get(‘score’,[])
account = db.get(‘account’,{‘admin’:‘123456’})
反序列化:
db[‘score’] = scores
db[‘account’] = {‘admin’:npass}
练习代码如下:

import shelve
import random

with shelve.open('db') as db:
    scores = db.get('score',[])
    account = db.get('account',{'admin':'123456'})
    print(scores)
    for i in range(3):
        name = input('输入用户名:')
        password = input('输入密码:')
        if name in account and password == account[name]:
            name = input("name:")
            cj = random.randint(50, 100)
            scores.append([name,cj])
            #npass=input("new password:")
            npass = input('请输入新密码:')
            db['score'] = scores
            db['account'] = {'admin':npass}
            break
        else:
            print ('用户名密码输入错误!')
    else:
        print('三次机会用完!')

运行shelve模块后会生成三个文件:
在这里插入图片描述
嗯嗯`~就酱紫!还有最后一个json模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值