zoukankan      html  css  js  c++  java
  • (二十二)序列化-pickle,json

    一、什么是序列化

     在我们存储数据或者⽹络传输数据的时候需要对我们的对象进⾏处理把对象处理成⽅便存储和传输的数据格式这个过程叫序列化不同的序列化结果也不同但是⽬的是⼀样的都是为了存储和传输

    二、 pickle

     pickle⽤起来很简单说⽩了就是把我们的python对象写入到⽂件中的⼀种解决⽅案但是写入到⽂件的是bytes,所以这东⻄不是给⼈看的是给机器看的

    import pickle
    
    class Cat:
    
      def __init__(self, name, age):
    
        self.name = name
    
        self.age = age
    
      def catchMouse(self):
    
        print(self.name, "抓⽼⿏")
    
     
    
    c = Cat("jerry", 18)
    
    bs = pickle.dumps(c) # 序列化⼀个对象.
    
    print(bs) # ⼀堆⼆进制. 看不懂
    
    cc = pickle.loads(bs) # 把⼆进制反序列化成我们的对象
    
    cc.catchMouse() # 猫依然是猫. 还可以抓⽼⿏
    

      

    pickle中的dumps可以序列化⼀个对象. loads可以反序列化⼀个对象. 我们使⽤dump

    还可以直接把⼀个对象写入到⽂件中

    # f = open("cat", mode="wb")
    
    # pickle.dump(c, f) # 写⼊到⽂件中
    
    # f.close()
    
    f = open("cat", mode="rb")
    
    cc = pickle.load(f) # 从⽂件中读取对象
    
    cc.catchMouse()
    

      

    要是存储多个对象呢,很简单,装list然后读取和写入都⽤list。

    lst = [Cat("jerry", 19), Cat("tommy", 20), Cat("alpha", 21)]
    
    f = open("cat", mode="wb")
    
    pickle.dump(lst, f)
    
    f = open("cat", mode="rb")
    
    ll = pickle.load(f)
    
    for el in ll:
    
       el.catchMouse()
    

      

    三、 json

     终于到json, json是我们前后端交互的枢纽相当于编程界的普通话⼤家沟通都⽤json。为什么这样呢? 因为json的语法格式可以完美的表⽰出⼀个对象那什么是json?json全称javascript object notation,翻译过来叫js对象简谱看看使用方法:

    import json
    
    dic = {"a": "张三", "b": "李四", "c": "王五"}
    
    s = json.dumps(dic) # 把字典转化成json字符串
    
    print(s)
    
    结果:
    
    {"a": "u5f20u4e09", "b": "u674eu56db", "c": "u738bu4e94"}

    结果很不友好啊那如何处理中⽂呢? dumps的时候给出另⼀个参数ensure_ascii=False就可以了。

    import json
    
    dic = {"a": "张三", "b": "李四", "c": "王五"}
    
    s = json.dumps(dic,ensure_ascii=False) # 把字典转化成json字符串
    
    print(s)
    
    运行后结果:
    
    {"a": "张三", "b": "李四", "c": "王五"}
    

      

    上面是把字典转为了字符串,下面再转回来:

    import json
    s = '{"a": "张三", "b": "李四", "c": "王五"}'
    dic = json.loads(s)
    print(type(dic), dic)
    
    结果:
    
    <class 'dict'> {'a': '张三', 'b': '李四', 'c': '王五'}
    

      

    很简单的就把数据转过来了,两个函数,dumps  loads 实现了互相的转换。

    同样也可以从⽂件中读取⼀个json:

    dic = {"a": "张三", "b": "李四", "c": "王五"}
    
    f = open("test.json", mode="w", encoding="utf-8")
    
    json.dump(dic, f, ensure_ascii=False) # 把对象打散成json写⼊到⽂件中
    
    f.close()

    同样也可以从⽂件中读取⼀个json:

    f = open("test.json", mode="r", encoding="utf-8")
    
    dic = json.load(f)
    
    f.close()
    
    print(dic)
  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/asia-yang/p/10187147.html
Copyright © 2011-2022 走看看