• 二十五、python中pickle序列学习(仅python语言中有)


    1.pickle序列介绍:提供4个关键字:dumps,dump,loads,load

      语法:f.write(pickle.dumps(dict))=pickle.dump(dict,f)

         "r=pickle.loads(f.read())" = "r=pickle.load(f)

      将字典存入文件,由于字典格式不能转化成字节格式的,所以引入pickle

      A.将一个字典转化成pickle序列存入一个文件


    import pickle
    #import cPickle as pickle

    accounts={
    1000:{
    'name':'Tom',
    'email':'22016311@qq.com',
    'passwd':'abc123',
    'balance':20000,
    'phone':'44165465',
    'bank_acc':{
    'ICBC':165116161,
    'CBC':16516516516,
    'ABC':656151616516
    }
    },
    1001:{
    'name':'Tom1',
    'email':'220116311@qq.com',
    'passwd':'abc1123',
    'balance':210000,
    'phone':'441654165',
    'bank_acc':{
    'ICBC':16511161611,
    'CBC':165165165161,
    'ABC':6561516165161
    }
    }
    }

    print (pickle.dumps(accounts)) #将字典转成pickle序列
    f=open("account.db","wb") #写入文件account.db
    f.write(pickle.dumps(accounts)) #写入文件account.db
    #pickle.dump(accounts,f)
    f.close()
    -------------------------------------------------------------------
    b'x80x03}qx00(Mxe8x03}qx01(Xx04x00x00x00nameqx02Xx03x00x00x00Tomqx03Xx05x00x00x00emailqx04Xx0fx00x00x0022016311@qq.comqx05Xx06x00x00x00passwdqx06Xx06x00x00x00abc123qx07Xx07x00x00x00balanceqx08M NXx05x00x00x00phoneq Xx08x00x00x0044165465q Xx08x00x00x00bank_accqx0b}qx0c(Xx04x00x00x00ICBCq Jx01yxd7 Xx03x00x00x00CBCqx0ex8ax05xa4 vxd8x03Xx03x00x00x00ABCqx0fx8ax06x04x1cxafxc5x98x00uuMxe9x03}qx10(hx02Xx04x00x00x00Tom1qx11hx04Xx10x00x00x00220116311@qq.comqx12hx06Xx07x00x00x00abc1123qx13hx08JP4x03x00h X x00x00x00441654165qx14hx0b}qx15(h x8ax05x0bU$xd8x03hx0ex8ax05ijx9ct&hx0fx8ax06)x18xd7xb8xf7x05uuu.'

      B.重新创建一个新的python文件读取account.db中的内容:

    import pickle

    account_file=open("account.db","rb") #打开文件
    account_dict=pickle.loads(account_file.read()) #以pickle加载文件
    #account_dict=pickle.load(account_file)
    #print (account_dict)
    account_file.close()
    #print(type(account_dict[1000]['balance']))
    account_dict[1000]['balance'] -= 500 #取字典中balance然后减500
    print (account_dict)

    f=open("account.db","wb")
    f.write(pickle.dumps(account_dict)) #将变更后的内容dumps入文件
    f.close()
    print (account_dict)
    -----------------------------------------------------------------------------

    {1000: {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, 1001: {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}
    {1000: {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, 1001: {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}

  • 相关阅读:
    php练习题之裁判打分
    OPENSUSE:安装方式
    openSUSE:下载帮助
    安装mysql时出现HTTP 500 内部服务…
    关于wamp启动是80端口被占用的问题…
    推荐15款好用的JS开发工具
    u盘美化
    wordpress怎么修改地址栏小图标
    显示桌面图标不见了
    站长必备的131个高权重外链库(转…
  • 原文地址:https://www.cnblogs.com/chushujin/p/9405210.html
Copyright © 2020-2023  润新知