pickle的简单例子
时间:2010-07-13 来源:wuliangxx
写了一个pickle模块的最简单的例子,备份着,免得总忘。
#! /usr/bin/env python # -*- coding: utf-8 -*- """ @author: wul @contact: [email protected] @date: Jul 13, 2010 1:45:59 PM @version: 0.0.0 @license: Copyright alibaba-inc.com @copyright: Copyright alibaba-inc.com """ import pickle class Person(object): def __init__(self, _name): self.name = _name if __name__ == "__main__": me = Person("wuliang") data = {"a":[1, 2, 3], "b":(u"吴亮", "Wu Liang"), "c":me} # 写入data.pkl文件 output = open("data.pkl", "wb") pickle.dump(data, output) output.close() # 从data.pkl文件中将对象读出 output = open("data.pkl", "rb") new_data = pickle.load(output) print new_data["a"] print new_data["b"][0] print new_data["c"].name output.close()
相关阅读 更多 +