美文网首页
Python常用操作

Python常用操作

作者: flyingsoul | 来源:发表于2016-04-08 22:35 被阅读0次

    将数据保存到文件

    import pickle
    # An arbitrary collection of objects supported by pickle.
    data = { 
            'a': [1, 2.0, 3, 4+6j], 
            'b': ("character string", b"byte string"), 
            'c': {None, True, False}
    }
    with open('data.pickle', 'wb') as f: 
        # Pickle the 'data' dictionary using the highest protocol available.
        pickle.dump(data, f, pickle.HIGHEST_PROTOCO)
    
    import pickle
    with open('data.pickle', 'rb') as f: 
        # The protocol version used is detected automatically, so we do not # have to specify it. 
        data = pickle.load(f)
    

    相关文章

      网友评论

          本文标题:Python常用操作

          本文链接:https://www.haomeiwen.com/subject/vdgvlttx.html