序列化模块

# 模块: 一个py文件就是一个模块.
'''
    python开发效率之高:Python的模块非常多,第三方库.
    模块分类:
        1,内置模块:登录模块,时间模块,sys模块,os模块 等等.
        2,扩展模块. itchat 微信有关.爬虫: beautifulsoup
            所有的扩展模块:https://pypi.org/
        3,自定义模块.自己写的py文件.

'''
# 序列化模块.
#序列化:创造一个序列.
#实例化:创造一个实例(对象).
# 将一个字典通过网络传输给另一个人.
'''
文件中可以存储:字符串,和bytes.
数据的传输:bytes类型.
'''
#  序列化: 创造一个序列, ---> 特殊处理(序列化的)字符串.

#序列化:
        # json:
        #         适用于不同语言之间的,
        #         但是可支持的数据类型:字符串,数字,列表(元祖),字典,float,bool,None 
        # pickle:
            # 只用于python语言之间的.
            #可支持python所有的数据类型.
        # shelve(了解):只是python,小工具(文件方面).

#序列化过程: 一个数据类型 ---> 序列化的字符串
#反序列化过程: 序列化的字符串  --->  它所对应的数据类型
# dumps  loads 网络的传输
# dic = {"alex": ['women','women','老女人'],'p1':True}
# dic = {"alex": ('women','women','老女人')}
# print(str(dic))  # 基础数据类型str  里面如果有引号就是单引号
# ret = json.dumps(dic,ensure_ascii=False) # 序列化过程:数据类型dic---> 序列化的字符串
# print(ret,type(ret))
# 被json序列化的字符串:
#1,可以直接通过网络互相传输.
#2,可以在各个语言中通用.
# dic1 = json.loads(ret)  # 反序列化过程.:将序列化的字符串---> 原有的数据类型.
# print(dic1,type(dic1))


#dump  load 有关文件存储
# import json
# l1 = ['张三','历史','王五','alex','老土','旭哥']
# f = open('json_file',encoding='utf-8',mode='w')
# json.dump(l1,f,ensure_ascii=False)  # 将序列化的字符串存储到文件中
# f.close()

# f = open('json_file',encoding='utf-8')
# ret = json.load(f)
# print(ret,type(ret))
# f.close()
# 有关文件存储的问题?
import json # dic = {"alex": ('women','women','老女人')} # dic2 = {"alex1": ('women','women','老女人')} # dic3 = {"alex2": ('women','women','老女人')} # f = open('json_files',encoding='utf-8',mode='w') # json.dump(dic,f,ensure_ascii=False) # json.dump(dic2,f,ensure_ascii=False) # json.dump(dic3,f,ensure_ascii=False) # f.close() # f = open('json_files', encoding='utf-8',) # print(json.load(f)) # print(json.load(f)) # print(json.load(f)) # f.close()
#
将多个序列化的字符串写入文件,然后反序列化,就会出错 # 用 dump load 只能写入和读取文件 一个序列化的字符串
# 用dumps和loads操作
import json
#
dic = {"alex": ('women','women','老女人')} # dic2 = {"alex1": ('women','women','老女人')} # dic3 = {"alex2": ('women','women','老女人')} # with open('json_files',encoding='utf-8',mode='a') as f1: # s1 = json.dumps(dic,ensure_ascii=False) # f1.write(s1+'\n') # s2 = json.dumps(dic2,ensure_ascii=False) # f1.write(s2+'\n') # s3 = json.dumps(dic3,ensure_ascii=False) # f1.write(s3+'\n') # # with open('json_files',encoding='utf-8') as f2: # for line in f2: # dic = json.loads(line) # print(dic,type(dic))
# 其他参数
# import json
# data = {'username':['李华','二愣子'],'sex':'male','age':16,'A':666}
# json_dic2 = json.dumps(data,sort_keys=True,indent=2,separators=('|','*'),ensure_ascii=False)
# print(json_dic2)
#
# print(json.loads(json_dic2)) # 如果改了:separators=('|','*')反序列化不行了
# sort_keys=True 字典键的首字母的ascii码排序
# ensure_ascii=False 显示中文
# indent=2 key 缩进

# dic = {(1,2,3):'alex',1:[1,2,3]}
# ret = json.dumps(dic)
# print(ret)  # TypeError: keys must be a string
# dumps  loads  网络传输
# dic = {1:True,(2,3):[1,2,3,4],False:{1,2,3}}
# import pickle
# ret = pickle.dumps(dic)  # bytes类型无法识别内容
#
# dic1 = pickle.loads(ret)
# print(dic1,type(dic1))

# dump  load 文件操作
# dic = {1:True,(2,3):[1,2,3,4],False:{1,2,3}}

# import pickle
# with open('pickle_file',mode='wb') as f1:
#     pickle.dump(dic,f1)

# with open('pickle_file',mode='rb') as f2:
#     print(pickle.load(f2))
# 多个数据存储到一个文件 (dump.load)
# dic = {"alex": ('women','women','老女人')}
# dic2 = {"alex1": ('women','women','老女人')}
# dic3 = {"alex2": ('women','women','老女人')}

# import pickle
# with open('pickle_files',mode='wb') as f1:
#     pickle.dump(dic,f1)
#     pickle.dump(dic2,f1)
#     pickle.dump(dic3,f1)
#     pickle.dump(dic3,f1)


# with open('pickle_files',mode='rb') as f1:
#     while True:
#         try:
#             print(pickle.load(f1))
#         except EOFError:
#             break
shelve 与文件相关
#
import shelve # f = shelve.open('shelve_file') # f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'} #直接对文件句柄操作,就可以存入数据 # f.close() # import shelve # f1 = shelve.open('shelve_file') # existing = f1['key'] #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错 # f1.close() # print(existing)

 

posted @ 2018-07-06 12:10  小小猎魔人  阅读(184)  评论(0编辑  收藏  举报