python20模块 序列化

序列化

 

'''
序列化:非字符串格式数据转成字符串
数据由单引号变成双引号
'''
import json
dic={'k1':'v1','k2':'v2','k3':'v3'}
str_dic=json.dumps(dic)
print(type(str_dic),str_dic)
#转回原有格式
dic2=json.loads(str_dic)
print(type(dic2),dic2)

 

'''
pickle的使用循环写入循环读取
存入文件的是bytes
'''
import pickle,time
struct_time1=time.localtime(1000000000)
struct_time2=time.localtime(2000000000)
f=open('pickle_file','wb')
pickle.dump(struct_time1,f)
pickle.dump(struct_time2,f)
f.close()
#读取
f=open('pickle_file','rb')
struct_time1=pickle.load(f)
struct_time2=pickle.load(f)
print(struct_time1.tm_year)
print(struct_time2.tm_year)
f.close()

 模块的导入

 

 

'''

使用别名 import time as t 导入特定类 from math import pi __all__只能约束from * 导入所有__all__中的类,如果没有all,导入所有 from math import * 1;模块尽量写在最上面 '''
'''
模块被执行时,__name__等于__main__
模块被调用时不等于

'''
 

 

posted @ 2019-05-13 15:03  jiuchen  阅读(71)  评论(0)    收藏  举报