序列化模块json;pickle;shelve

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

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

# 下面的解决方式不好! eval极度不安全.

# dic = {'alex':['women','women','oldwomen']}
# ret = str(dic)
# # print(ret,type(ret))
# b1 = ret.encode('utf-8')
# s1 = b1.decode('utf-8') # "{'alex':['women','women','oldwomen']}"
# dic1 = eval(s1)
# print(dic1,type(dic1))

#那怎么解决?

# 序列化: 创造一个序列, ---> 特殊处理(序列化的)字符串.

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

# json:
# 数据通过网络发送给别人. json
# 写入文件 也用到json.

#序列化过程: 一个数据类型 ---> 序列化的字符串
#反序列化过程: 序列化的字符串 ---> 它所对应的数据类型

import json

# 两对:
# dumps loads
# dump load

# 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))
import json
l1 = ['张三','历史','王五','alex','老土','旭哥']

#dump load 有关文件存储
# 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()

# 有关文件存储的问题?
dic = {"alex": ('women','women','老女人')}
dic2 = {"alex1": ('women','women','老女人')}
dic3 = {"alex2": ('women','women','老女人')}

# 将多个序列化的字符串写入文件,然后反序列化,就会出错
# 用 dump load 只能写入和读取文件 一个序列化的字符串
# 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()

# 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))

# dic = {"alex": ('women','women','老女人')}
# ret = "\n"+json.dumps(dic)
# print(ret)

# 其他参数
# 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

#pickle 序列化模块,python语言网络交互使用的,他支持所有的python数据类型.
# 两对 四个方法

# 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))

# 多个数据存储到一个文件
# 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)

# import shelve
# f = shelve.open('shelve_file', flag='r')
# # existing = f['key']
# f['key'] = [11,22,33]
# f.close()
# # print(existing)
#
# f = shelve.open('shelve_file', flag='r')
# ex = f['key']
# print(ex)
# f.close()

# f['key'] = {'int':10, 'float':9.5, 'string':'Sample data','new_value': 'this was not here before'}
# import shelve
# f1 = shelve.open('shelve_file')
# print(f1['key'])
# f1['key']['new_value'] = 'this was not here before'
# print(f1['key'])
# f1.close()

# f2 = shelve.open('shelve_file',writeback=True)
# print(f2['key'])
# f2['key']['new_value'] = 'this was not here before'
# print(f2['key'])
# f2.close()

import shelve
# f1 = shelve.open('shelve_file')
# f1['key'] = {'int':10, 'float':9.5, 'string':'Sample data'}
# print(f1['key'])
# f1.close()
# 添加了一个键值对,主要是添加了一个值 {'int':10, 'float':9.5, 'string':'Sample data'}

# f1 = shelve.open('shelve_file')
# print(f1['key'])
# f1['key']['new_value'] = 'this was not here before'
# print(f1['key'])
# f1.close()

# f2 = shelve.open('shelve_file',writeback=True)
# print(f2['key'])
# f2['key']['new_value'] = 'this was not here before'
# print(f2['key'])
# f2.close()





猜你喜欢

转载自www.cnblogs.com/pypy35/p/9271783.html