标签:
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块 marshal(序列化) #来自:mldxs import marshal data1 = [‘abc‘,12,23,‘jb51‘] data2 = {1:‘aaa‘,"b":‘dad‘} data3 = (1,2,4) output_file = open("a.txt",‘wb‘)#把这些数据序列化到文件中,注:文件必须以二进制模式打开 marshal.dump(data1,output_file) marshal.dump(data2,output_file) marshal.dump(data3,output_file) output_file.close() input_file = open(‘a.txt‘,‘rb‘)#从文件中读取序列化的数据 #data1 = [] data1 = marshal.load(input_file) data2 = marshal.load(input_file) data3 = marshal.load(input_file) print data1#给同志们打印出结果看看 print data2 print data3 print ‘------------‘ outstring = marshal.dumps(data1)#marshal.dumps()返回是一个字节串,该字节串用于写入文件 open(‘out.txt‘,‘wb‘).write(outstring) file_data = open(‘out.txt‘,‘rb‘).read() real_data = marshal.loads(file_data) print real_data
标签:
原文地址:http://www.cnblogs.com/dengyg200891/p/4986311.html