zoukankan      html  css  js  c++  java
  • 041 文件 持久化

    pickle

    • 序列化(持久化,落地):把程序运行中的信息保存在磁盘上
    • 反序列化: 序列化的逆过程
    • pickle: Python 提供的序列化模块
      • pickle.dump: 序列化
      • pickle.load: 反序列化

    举例

    >>> import pickle  # 下面的例子就不重复这个导入操作了
    >>> 
    

    “腌制泡菜”

    >>> age = 18
    >>> with open("text.txt", "wb") as f:
    ...   pickle.dump(age, f)
    >>> 
    
    # text.txt
    €K.
    

    “取出泡菜”

    >>> f = open("text.txt", "rb")  # 这里用二进制是必要的
    >>> f.readline()
    b'x80x03Kx12.'
    >>> f.seek(0)
    0
    >>> pickle.load(f)
    18
    >>> 
    

    序列化

    >>> lst = [18, 19, 20, "york", "fish", [2, 3]]
    >>> with open("text.txt", "wb") as f:
    ...   pickle.dump(lst, f)
    ...
    >>> 
    

    反序列化

    >>> with open("text.txt", "rb") as f:
    ...   pickle.load(f)
    ...
    [18, 19, 20, 'york', 'fish', [2, 3]]
    >>> 
    

    注意事项

    1. pickle 几乎可以把所有 Python 的对象都转换为二进制的形式存放
    2. pickle 序列化,需要以二进制打开
    3. 若要将 pickle 后的内容保存为文件,推荐以 .pkl.pickle 为后缀

    shelve

    • 持久化工具
    • 存取方式类似字典,用 k, v 保存数据
    >>> import shelve  # 下面的例子就不重复这个导入操作了
    >>> 
    

    举例

    >>> shv = shelve.open("shv.db")
    >>> shv["one"] = 1
    >>> shv["two"] = 2
    >>> shv["three"] = 3
    >>> shv.close()
    >>> 
    
    • Windows 下,上述操作过后,相应文件夹内出现三个文件
      • shv.db.bak
      • shv.db.dat
      • shv.db.dir
    >>> shv = shelve.open("shv.db")
    >>> try:
    ...   print(shv["one"])
    ...   print(shv["four"])
    ... except KeyError as e:
    ...   print(e)
    ... finally:
    ...   shv.close()
    ...
    1
    b'four'
    >>> 
    

    特性

    • 不支持多个应用并行写入
      • 解决方法:open 的时候可以使用 flag=r
    • 写回问题
      • shelv 默认情况下,不会接受持久化对象进行任何修改
      • 解决方法:强制写回 writeback=True
    >>> shv = shelve.open("shv.db", flag='r')
    >>> try:
    ...   k = shv["one"]
    ...   print(k)
    ... finally:
    ...   shv.close()
    ...
    1
    >>> 
    
    >>> shv = shelve.open("shv.db")
    >>> try:
    ...   shv["one"] = {'a':65, 'b':98, 'c':99}
    ... finally:
    ...   shv.close()
    ...
    >>> shv = shelve.open("shv.db")
    >>> try:
    ...   one = shv["one"]
    ...   print(one)
    ... finally:
    ...   shv.close()
    ...
    {'a': 65, 'b': 98, 'c': 99}
    >>> 
    

    强制写回

    问题

    • shelve 关闭,内容仍存于内存中,没有写回数据库
    >>> shv = shelve.open("shv.db")
    >>> try:
    ...   k = shv["one"]
    ...   print(k)
    ...   k['a'] = 97
    ... finally:
    ...   shv.close()
    ...
    {'a': 65, 'b': 98, 'c': 99}
    >>> 
    

    解法

    >>> shv = shelve.open("shv.db", writeback=True)
    >>> try:
    ...   k = shv["one"]
    ...   print(k)
    ...   k['a'] = 97
    ... finally:
    ...   shv.close()
    ...
    {'a': 65, 'b': 98, 'c': 99}
    >>> shv = shelve.open("shv.db")
    >>> try:
    ...   print(shv["one"])
    ... finally:
    ...   shv.close()
    ...
    {'a': 97, 'b': 98, 'c': 99}
    >>> 
    

    使用 with 管理上下文环境

    >>> with shelve.open("shv.db", writeback=True) as shv:
    ...   print(shv["one"])
    ...   shv["one"]['a'] = 0
    ...
    {'a': 97, 'b': 98, 'c': 99}
    >>> with shelve.open("shv.db") as shv:
    ...   print(shv["one"])
    ...
    {'a': 0, 'b': 98, 'c': 99}
    >>> 
    
  • 相关阅读:
    屏幕内跟随鼠标移动(鼠标点击一个位置,物体移动到该位置)
    su root认证失败的解决方法
    centos yum换阿里云源
    用户名 不在 sudoers文件中
    WebApi防重复提交方案
    解决Ubuntu安装openssh-server依赖问题
    # git 操作拾遗
    编译原理-词法分析05-正则表达式到DFA-01
    Content Negotiation in ASP.NET Web API
    Model Validation in ASP.NET Web API
  • 原文地址:https://www.cnblogs.com/yorkyu/p/12088168.html
Copyright © 2011-2022 走看看