• python json、pickle


    文章部分转自:https://www.cnblogs.com/lincappu/p/8296078.html

    json:用于字符串和Python数据类型间进行转换
    pickle: 用于python特有的类型和python的数据类型间进行转换
    json提供四个功能:dumps,dump,loads,load
    pickle提供四个功能:dumps,dump,loads,load

    1. pickle

    pickle可以存储什么类型的数据呢?

    1. 所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串,字节,None。
    2. 由任何原生类型组成的列表,元组,字典和集合。
    3. 函数,类,类的实例

    pickle模块中常用的方法有:

        1. pickle.dump(obj, file, protocol=None,)

        必填参数obj表示将要封装的对象

        必填参数file表示obj要写入的文件对象,file必须以二进制可写模式打开,即“wb”

        可选参数protocol表示告知pickler使用的协议,支持的协议有0,1,2,3,默认的协议是添加在Python 3中的协议3。   

    • Protocol version 0 is the original “human-readable” protocol and is backwards compatible with earlier versions of Python.
    • Protocol version 1 is an old binary format which is also compatible with earlier versions of Python.
    • Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes. Refer to PEP 307 for information about improvements brought by protocol 2.
    • Protocol version 3 was added in Python 3.0. It has explicit support for bytes objects and cannot be unpickled by Python 2.x. This is the default protocol, and the recommended protocol when compatibility with other Python 3 versions is required.
    • Protocol version 4 was added in Python 3.4. It adds support for very large objects, pickling more kinds of objects, and some data format optimizations. Refer to PEP 3154 for information about improvements brought by protocol 4. 

        2. pickle.load(file,*,fix_imports=True, encoding="ASCII", errors="strict")

        必填参数file必须以二进制可读模式打开,即“rb”,其他都为可选参数

        3. pickle.dumps(obj):以字节对象形式返回封装的对象,不需要写入文件中

        4. pickle.loads(bytes_object): 从字节对象中读取被封装的对象,并返回

     pickle模块可能出现三种异常:

        1. PickleError:封装和拆封时出现的异常类,继承自Exception

        2. PicklingError: 遇到不可封装的对象时出现的异常,继承自PickleError

        3. UnPicklingError: 拆封对象过程中出现的异常,继承自PickleError

    import pickle
    data = ['aa', 'bb', 'cc']
    
    # 1. dumps(object) -> string;将数据转成只有python语言认识的字符串
    p_str = pickle.dumps(data)
    print(p_str)
    # b'x80x03]qx00(Xx02x00x00x00aaqx01Xx02x00x00x00bbqx02Xx02x00x00x00ccqx03e.
    
    # 2. dump(object, file); 将数据转成pickle的字符串,并写入文件
    with open('ak.pk', 'wb') as f:  # file必须以二进制可写模式打开,即“wb”
        pickle.dump(data, f)        # 文件里面存的是pickle的字符串,不是明文数据
    
    # 3. load(file) -> object ;从文件读取pickle的字符串并转换成原来的数据格式
    with open('ak.pk', 'rb') as f:  # 要以二进制读取打开
        data = pickle.load(f)
        print(data)          # data是明文数据
    # ['aa', 'bb', 'cc']
    
    # 4. loads(string) -> object;将pickle字符串转成python数据
    mes = pickle.loads(p_str)
    print(mes)
    # ['aa', 'bb', 'cc']

    2. Json

    方法和pickle一样,dumps、loads是对json字符串的操作,dump、load是对文件的操作

    import json
    
    s = {1:'a',2:'b',3:'c'}
    # 1. dumps: obj-->string;从数据转成json字符串
    ds = json.dumps(s)
    print(ds,type(ds))
    
    # 2. dump: obj-->file;从数据写入文件
    with open('j','w') as f:
        d = json.dump(s,f)
    
    # 3. loads: string-->obj;从json字符串转成数据
    lds = json.loads(ds)
    print(lds,type(lds))
    
    # 4. load: file-->obj;从文件读取转成数据
    with open('j','r') as f:
        ld = json.load(f)
        print(ld,type(ld))
  • 相关阅读:
    线程锁(互斥锁Mutex)及递归锁
    threading模块,python下的多线程
    笔试面试题实现
    Socket编程,SocketServer模块
    iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
    iOS 根据文件名获取到文件路径
    IOS UILabel自动换行
    🐤小记
    iOS 获取当前月份的天数(转)、
    IOS 在Ipad 横屏 上使用UIImagePickerController
  • 原文地址:https://www.cnblogs.com/wztshine/p/11765786.html
Copyright © 2020-2023  润新知