• day023 常用模块02


    1.pickle

    1.  import pickle  #序列化
    d=pickle.dumps(object#序列化   一堆二进制
    ds=pickle.loads(d)      #反序列化
    # 将序列化的对象写入文件  dump
    f=open("cat",mode="wb")
    pickle.dump(object,f)
    f.close()
    # 将文件中的序列化读出来   load
    f=open("cat",mode="rb")
    c=pickle.load(f)   #反序列化出来的对象
    # 读多个内容的时候将内容放入列表中然后读写列表

    2.# shelve 提供持久化操作,比较像在操作字典
    import shelve
    ret=shelve.open("filename"#ret相当于一个字典,但是ret仍然是一个文件
    print(ret["key"])  #取得value

    ret=shelve.open("filename",writeback=True)
    #加上writeback=Ture 则可以操作字典的增删改,没有则不能改变

    3.jison,将字典转换成字符串
    import json
    dic={"a":"女王","b":"小清新"}
    s=json.dumps(dic,ensure_ascii=False#将字典转换成字符串
    print(s)

    # 将前端的信息转换成字典
    import json
    s="{a:女王,b:小清新}"
    dic=json.loads(s)

    # json可以将序列化的结果写入文件中
    # json写入文件中读写入文件不用dump,load,写入可以写入,但是多个字典时没有办法读取,
    # 需要一行一行的写入,一行一行的读取,每一行加" "换行分开
    import json
    lst=[{"a":1},{"b":2},{"c":3}]
    # 写入
    f=open("test",mode="w",encoding="utf-8")
    for el in lst:
        s=json.dumps(el,ensure_ascii=True)+" "
       
    f.write(s)
    f.close()
    # 读取
    f=open("test",mode="r",encoding="utf-8")
    for el in f:
        dic=json.loads(el,ensure_ascii=True)
        print(dic)
    f.close()

    4.configparser 模块
     
    import configparser
      config=configparser.ConfigParser()  #操作字典
     
    f=open("test.ini",mode="w")
      config.write(f)  #写入文件
     
    config.read("test.ini"#读取文件
     
    config.sections()  #获取章节
      # 最后操作字典

  • 相关阅读:
    【异常】org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter '**' not found.的解决办法
    java如何进行字符串拼接?
    poj2352消防站
    NOIP第7场模拟赛题解
    转载:
    usaco 2010年3月银组题解
    Js 向json对象中添加新元素
    List<T>中 GetRange (int index, int count)的使用
    C# string格式的日期时间字符串转为DateTime类型
    C# DataTable转List<T>--利用反射
  • 原文地址:https://www.cnblogs.com/litieshuai/p/9761685.html
Copyright © 2020-2023  润新知