How can I use pickle to save a dict?

前端 未结 9 2345
说谎
说谎 2020-11-22 06:45

I have looked through the information that the Python docs give, but I\'m still a little confused. Could somebody post sample code that would write a new file then use pickl

9条回答
  •  日久生厌
    2020-11-22 07:44

    >>> import pickle
    >>> with open("/tmp/picklefile", "wb") as f:
    ...     pickle.dump({}, f)
    ... 
    

    normally it's preferable to use the cPickle implementation

    >>> import cPickle as pickle
    >>> help(pickle.dump)
    Help on built-in function dump in module cPickle:
    
    dump(...)
        dump(obj, file, protocol=0) -- Write an object in pickle format to the given file.
    
        See the Pickler docstring for the meaning of optional argument proto.
    

提交回复
热议问题