zoukankan      html  css  js  c++  java
  • pickle与shelve

    pickle Example

    写入文件

    import pickle
    
    integers = [1, 2, 3, 4, 5]
    
    with open('pickle-example.p', 'wb') as pfile:
        pickle.dump(integers, pfile)

    读取文件

    import pickle
    
    with open('pickle-example.p', 'rb') as pfile:
        integers = pickle.load(pfile)
        print integers

    shelve Example

    写入文件

    import shelve
    
    integers = [1, 2, 3, 4, 5]
    
    # If you're using Python 2.7, import contextlib and use
    # the line:
    # with contextlib.closing(shelve.open('shelf-example', 'c')) as shelf:
    with shelve.open('shelf-example', 'c') as shelf:
        shelf['ints'] = integers

    读取文件

    import shelve
    
    # If you're using Python 2.7, import contextlib and use
    # the line:
    # with contextlib.closing(shelve.open('shelf-example', 'r')) as shelf:
    with shelve.open('shelf-example', 'r') as shelf:
        for key in shelf.keys():
            print(repr(key), repr(shelf[key])))
  • 相关阅读:
    五月杂题选做
    BJOI 2021 游记&题解
    U149858
    CF1037简要题解
    CF Round706简要题解
    联合省选 2020
    九省联考 2018 IIIDX
    九省联考 2018 秘密袭击
    AGC006F Balckout
    概率生成函数学习笔记
  • 原文地址:https://www.cnblogs.com/zhanmeiliang/p/6103478.html
Copyright © 2011-2022 走看看