zoukankan      html  css  js  c++  java
  • 存储与取存储

    #!/usr/bin/python
    # Filename: pickling.py


    import cPickle as p
    #import pickle as p

    shoplistfile = 'shoplist.data'
    # the name of the file where we will store the object

    shoplist = ['apple''mango''carrot']

    # Write to the file
    f = file(shoplistfile, 'w')
    p.dump(shoplist, f) 
    # dump the object to a file
    f.close()

    del shoplist # remove the shoplist

    # Read back from the storage

    f = file(shoplistfile)
    storedlist = p.load(f)

    print storedlist

    首先,请注意我们使用了import..as语法。这是一种便利方法,以便于我们可以使用更短的模块名称。在这个例子中,它还让我们能够通过简单地改变一行就切换到另一个模块(cPickle或者pickle)!在程序的其余部分的时候,我们简单地把这个模块称为p

    为了在文件里储存一个对象,首先以写模式打开一个file对象,然后调用储存器模块的dump函数,把对象储存到打开的文件中。这个过程称为 储存 。

    接下来,我们使用pickle模块的load函数的返回来取回对象。这个过程称为 取储存 。

  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/nku-wangfeng/p/7690047.html
Copyright © 2011-2022 走看看