zoukankan      html  css  js  c++  java
  • 第二十二天- 序列化 pickle json shelve

    # 序列化:存储或传输数据时,把对象处理成方便存储和传输的数据格式,这个过程即为序列化
    # Python中序列化的三种方案:
    # 1.pickle python任意数据——》bytes写入⽂件;写好的bytes——》python的数据.
    # 2.shelve 简单另类的⼀种序列化⽅案. 可以作为⼀种⼩型的数据库来使⽤
    # 3.json 将字典列表转换成字符串,前后端数据交互高频使用的⼀种数据格式


    # pickle:
    # 写入到文件的是bytes

    # pickle中的dumps可以序列化⼀个对象.loads可以反序列化⼀个对象
     1 import pickle
     2 class Cat:
     3     def __init__(self,name,color):
     4         self.name = name
     5         self.color =color
     6 
     7     def chi(self):
     8         print("%s猫会吃老鼠"%(self.name))
     9 
    10 c = Cat("加菲","橘色")
    11 # c.chi()
    12 bs = pickle.dumps(c)  # 把对象转换成bytes
    13 # print(bs)  # b'x80x03c__main__
    Cat
    qx00)x81qx01}qx02(Xx04x00x00x00nameqx03Xx06x00x00x00xe5x8axa0xe8x8fxb2qx04Xx05x00x00x00colorqx05Xx06x00x00x00xe6xa9x98xe8x89xb2qx06ub.'
    14 
    15 # 把bytes反序列化成对象
    16 cc = pickle.loads(b'x80x03c__main__
    Cat
    qx00)x81qx01}qx02(Xx04x00x00x00nameqx03Xx06x00x00x00xe5x8axa0xe8x8fxb2qx04Xx05x00x00x00colorqx05Xx06x00x00x00xe6xa9x98xe8x89xb2qx06ub.')
    17 cc.chi()
    18 print(cc.name,cc.color)  # 加菲猫会吃老鼠  加菲 橘色

    # dump load读写文件操作
    # dump load ⼀个对象写读到文件
    1 c = Cat("加菲","橘色")
    2 pickle.dump(c,open("cat.dat",mode = 'wb'))  # 把对象写到文件  pickle.dump() 注意bytes用wb模式
    3 ccc = pickle.load(open('cat.dat',mode='rb'))  # 把对象读取出来  pickle.load() 注意bytes用rb模式
    4 ccc.chi()  # 加菲猫会吃老鼠
    5 print(ccc.name,ccc.color)  # 加菲 橘色

    # pickle用list读写多个对象
     1 c1 = Cat("加菲1","橘色")
     2 c2 = Cat("加菲2","橘色")
     3 c3 = Cat("加菲3","橘色")
     4 c4 = Cat("加菲4","橘色")
     5 c5 = Cat("加菲5","橘色")
     6 
     7 # lst = [c1,c2,c3,c4,c5]
     8 # f = open('cat.dat',mode='wb')
     9 # pickle.dump(lst,f)  # 把lst写到f
    10 
    11 f = open('cat.dat',mode='rb')
    12 lis = pickle.load(f)  # 把lis从f读取出来
    13 for cc in lis:
    14     cc.chi()

    # 应用:pickle实现注册登录
     1 import pickle
     2 
     3 
     4 class User:
     5     def __init__(self,username,password):
     6         self.username = username
     7         self.password = password
     8 
     9 
    10 class Client:
    11     # 注册
    12     def regist(self):
    13         uname = input("请输入你的账户:")
    14         pwd = input("请输入你的密码:")
    15         user = User(uname,pwd)  # 把uname pwd 传参到User类
    16         pickle.dump(user,open("userinfo",mode='ab'))  # 把对象保存到userinfo
    17         print("注册成功!")
    18 
    19     # 登录
    20     def login(self):
    21         uname = input("请输入你的账户:")
    22         pwd = input("请输入你的密码:")
    23         f = open('userinfo',mode='rb')  
    24         while 1:
    25             # 加入try except 当不存在的账户登录时 抛出错误
    26             try:
    27                 u = pickle.load(f)  # 从userinfo中读取出用户 
    28                 if u.username == uname and u.password == pwd:
    29                     print("登陆成功!")
    30                     break
    31             except Exception as e:
    32                 print("登录失败!")
    33                 break
    34 
    35 d = Client()
    36 # print("注册")
    37 # d.regist()
    38 # d.regist()
    39 # d.regist()
    40 # print("登录")
    41 # d.login()
    42 # d.login()
    43 # d.login()
    View Code


    # shelve 就是把数据写到硬盘上.操作shelve非常的像操作字典.
    1 import shelve
    2 
    3 d = shelve.open("test")  # 可理解成文件类型的字典
    4 # d['wf'] = '汪峰'
    5 d['wf'] = '王菲'
    6 print(d['wf'])  # 汪峰  王菲
    7 d.close()
    # 存储一些复杂点的数据
     1 import shelve
     2 d = shelve.open('test')
     3 d['wf'] = {'name':'汪峰','age':47,'wife':{'name':'章子怡','hobby':'拍电影'}}
     4 print(d['wf']) # 运行结果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '拍电影'}}
     5 d.close()
     6 
     7 # 尝试修改
     8 d = shelve.open('test')
     9 d['wf']['wife']['name'] = '小章'
    10 print(d['wf'])  # 运行结果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '拍电影'}}
    11 d.close()
    12 # 坑 由上可知 直接修改没效果 需用到 writeback
    # writeback=True 可动态的把修改的信息写入到⽂件中.
    1 d = shelve.open('test',writeback=True)  # 文件类型字典修改时 writeback 把修改回写到文件
    2 d['wf']['wife']['hobby'] = '搓麻将'  # 修改vlue
    3 d.close()
    4 
    5 d = shelve.open('test')
    6 print(d['wf'])  # 运行结果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '搓麻将'}}
    7 d.close()
    # json 前后端交互的纽带
    # json全称javascript object notation.翻译js对象简谱.
    # json代码(python中的字典)
     1 wf = {
     2     "name":"汪峰",
     3     "age":18,
     4     "hobby":"上头条",
     5     "wife":{
     6         "name":'⼦怡',
     7         "age":19,
     8         "hobby":["唱歌", "跳舞", "演戏"]
     9         }
    10 }
    # 直接把字典转化成json字符串
    import json
    1 dic = {'a':'一只萝莉','b':'两只萝莉','c':'一群萝莉','d':False,'e':None}
    2 s = json.dumps(dic,ensure_ascii=False)  # ensure_ascii 固定套路不转成bytes
    3 print(type(s))  # <class 'str'>
    4 print(s)  # {"a": "一只萝莉", "b": "两只萝莉", "c": "一群萝莉", "d": false, "e": null}
    # 把json字符串转换成字典
    1 s1 = '{"a": "一只萝莉", "b": "两只萝莉", "c": "一群萝莉", "d": false, "e": null}'
    2 d = json.loads(s1)
    3 print(d)  # {'a': '一只萝莉', 'b': '两只萝莉', 'c': '一群萝莉', 'd': False, 'e': None}
    4 print(type(d))  # <class 'dict'>
    # 把json写入文件
    1 dic = {'a':'小萝莉','b':'大萝莉','c':'一群萝莉','d':False,'e':None,'wf':{'name':'怒放的生命','hobby':'皮裤'}}
    2 f = open('loli.json',mode='w',encoding='utf-8')  # 注意:utf-8格式写入
    3 json.dump(dic,f,ensure_ascii=False,indent=4)  # indent=4 缩进4格 等同tab 便于阅读
    # 文件中读取json
    1 f = open('loli.json',mode='r',encoding="utf-8")
    2 d = json.load(f)
    3 # print(d)
    4 f.close()
    # 把对象转换成json
     1 class Person:
     2     def __init__(self,firstName,lastName):
     3         self.firstName = firstName
     4         self.lastName = lastName
     5 
     6 p = Person('尼古拉斯','赵四')
     7 
     8 # 方案一 转化的是字典
     9 s = json.dumps(p.__dict__,ensure_ascii=False)
    10 print(s)  # {"firstName": "尼古拉斯", "lastName": "赵四"}
    11 
    12 # 方案二 自己定义函数
    13 def func(obj):
    14     return {
    15         'firstName':obj.firstName,
    16         'lastName':obj.lastName
    17     }
    18 
    19 s = json.dumps(p,default=func,ensure_ascii=False)
    20 print(s)  # {"firstName": "尼古拉斯", "lastName": "赵四"}
    View Code
    # 函数把字典(json)转换成对象
    1 s = '{"firstName": "尼古拉斯", "lastName": "赵四"}'
    2 def func(dic):
    3     return Person(dic['firstName'],dic['lastName'])
    4 
    5 p = json.loads(s,object_hook=func)  # 字典转换成对象
    6 
    7 print(p.firstName,p.lastName)
    # 注意.我们可以向同⼀个⽂件中写⼊多个json串.但是读不⾏.
     1 import json
     2 lst = [{'a':1},{'b':2},{'c':3}]
     3 f = open('test.json',mode='w',encoding='utf-8')
     4 for el in lst:
     5     json.dump(el,f)
     6 f.close()
     7 # 若需要读取改用 dumps和loads  循环对每行分别处理
     8 import json
     9 lst = [{"a": 1}, {"b": 2}, {"c": 3}]
    10 # 写⼊
    11 f = open("test.json", mode="w", encoding="utf-8")
    12 for el in lst:
    13     s = json.dumps(el, ensure_ascii=True) + "
    "  # 注意dumps这里ensure_ascii是True
    14     f.write(s)
    15 f.close()
    16 # 读取
    17 f = open("test.json", mode="r", encoding="utf-8")
    18 for line in f:
    19     dic = json.loads(line.strip())
    20     print(dic)
    21 f.close()
    View Code
    # configparser模块(参考)
    # 模块用于配置⽂件格式与windows ini⽂件类似,可以包含⼀个或多个节(section)每个节可有多个参数(键=值)
    # 服务器配置文件
     1 '''
     2 [DEFAULT] [DEFAULT]
     3 ServerAliveInterval = 45
     4 Compression = yes
     5 CompressionLevel = 9
     6 ForwardX11 = yes
     7 [[bitbucket.org bitbucket.org]]
     8 User = hg
     9 [[topsecret.server.com topsecret.server.com]]
    10 Port = 50022
    11 ForwardX11 = no
    12 '''
    # ⽤configparser对这样的⽂件进⾏处理
    # 初始化
     1 import configparser
     2 
     3 config = configparser.ConfigParser()
     4 config['DEFAULT'] = {
     5 "sleep": 1000,
     6  "session-time-out": 30,
     7  "user-alive": 999999
     8 }
     9 config['TEST-DB'] = {
    10  "db_ip": "192.168.17.189",
    11  "port": "3306",
    12  "u_name": "root",
    13  "u_pwd": "123456"
    14 }
    15 config['168-DB'] = {
    16  "db_ip": "152.163.18.168",
    17  "port": "3306",
    18  "u_name": "root",
    19  "u_pwd": "123456"
    20 }
    21 config['173-DB'] = {
    22  "db_ip": "152.163.18.173",
    23  "port": "3306",
    24  "u_name": "root",
    25  "u_pwd": "123456"
    26 }
    27 f = open("db.ini", mode="w")
    28 config.write(f) # 写⼊⽂件
    29 f.flush()
    30 f.close()
    View Code
    # 读取文件信息
     1 config = configparser.ConfigParser()
     2 config.read("db.ini") # 读取⽂件
     3 print(config.sections()) # 获取到section. 章节...DEFAULT是给每个章节都配备的信息
     4 print(config.get("DEFAULT", "SESSION-TIME-OUT")) # 从xxx章节中读取到xxx信息
     5 # 也可以像字典⼀样操作
     6 print(config["TEST-DB"]['DB_IP'])
     7 print(config["173-DB"]["db_ip"])
     8 for k in config['168-DB']:
     9  print(k)
    10 for k, v in config["168-DB"].items():
    11  print(k, v)
    12 print(config.options('168-DB')) # 同for循环,找到'168-DB'下所有键
    13 print(config.items('168-DB')) #找到'168-DB'下所有键值对
    14 print(config.get('168-DB','db_ip')) # 152.163.18.168 get⽅法Section下的key对应的value
    View Code
    # 增删改操作
    # 先读取. 然后修改. 最后写回⽂件
     1 config = configparser.ConfigParser()
     2 config.read("db.ini") # 读取⽂件
     3 # 添加⼀个章节
     4 # config.add_section("189-DB")
     5 # config["189-DB"] = {
     6 # "db_ip": "167.76.22.189",
     7 # "port": "3306",
     8 # "u_name": "root",
     9 # "u_pwd": "123456"
    10 # }
    11 # 修改信息
    12 config.set("168-DB", "db_ip", "10.10.10.168")
    13 # 删除章节
    14 config.remove_section("173-DB")
    15 # 删除元素信息
    16 config.remove_option("168-DB", "u_name")
    17 # 写回⽂件
    18 config.write(open("db.ini", mode="w"))
    View Code


    
    
    
    
    
    



  • 相关阅读:
    微信app支付,服务端对接
    git 忽略文件权限
    linux 终端全局代理设置
    centos 谷歌浏览器安装
    putty快速设置本地代理
    centos rpmforge repo
    mysql 同步
    apscheduler 排程
    tornado 排程
    gedit 格式化json
  • 原文地址:https://www.cnblogs.com/xi1419/p/9965573.html
Copyright © 2011-2022 走看看