python中常用的序列化模块_python3-常用模块之序列化

本文深入探讨了Python中使用json模块进行数据序列化和反序列化的具体方法及注意事项,包括不同类型数据的处理方式及其限制,并展示了如何通过参数调整来优化输出格式。

1290440-20190917230937320-924364787.png

json

dumps loads

在内存中做数据转换 :

dumps 数据类型 转成 字符串 序列化

loads 字符串 转成 数据类型 反序列化

dump load

直接将数据类型写入文件,直接从文件中读出数据类型

dump 数据类型 写入 文件 序列化

load 文件 读出 数据类型 反序列化

json是所有语言都通用的一种序列化格式

只支持 列表 字典 字符串 数字

字典的key必须是字符串

序列化之后的json没有单引号,只有双引号,因为json只支持双引号

# import json

# 问题1 字典中的int类型,序列化及反序列化后变成字符串类型

# dic = {1: 'value', 2: 'value2'}

# ret = json.dumps(dic) # 序列化

# print(dic, type(dic))

# print(ret, type(ret))

# res = json.loads(ret) # 反序列化

# print(res, type(res))

# 问题2 元组变成了列表

# dic = {1: [1, 2, 3], 2: (4, 5, 'aa')}

# ret = json.dumps(dic) # 序列化

# print(dic, type(dic))

# print(ret, type(ret))

# res = json.loads(ret) # 反序列化

# print(res, type(res))

3A42529379DD4587B76DE1A569C1B318

1290440-20190923201247208-1530315219.png

# 问题3 集合类型不能作为键,序列化

# s = {1, 2, 'aaa'}

# json.dumps(s)

# 问题4 元组不能作为键 # TypeError: keys must be a string

# json.dumps({(1, 2, 3): 123})

# 向文件中记录字典

# dic = {'key1' : 'value1','key2' : 'value2'}

# ret = json.dumps(dic) # 序列化

# with open('json_file','w') as f:

# f.write(ret)

# # 从文件中读取字典

# with open("json_file") as f:

# content = f.read()

# ret = json.loads(content)

# print (ret,content)

# print (type(ret))

# print (type(content))

# load dump 直接操作文件

# dic = {'key1' : 'value1','key2' : 'value2'}

# with open("json_file","w") as f:

# json.dump(dic,f)

# with open("json_file") as f:

# ret = json.load(f)

# print (ret["key1"])

# 问题5 不支持连续的存 取

# dic = {'key1' : 'value1','key2' : 'value2'}

# with open('json_file','a') as f:

# json.dump(dic,f)

# json.dump(dic,f)

# json.dump(dic,f)

# with open('json_file','r') as f:

# dic = json.load(f)

# print(dic.keys())

# 需求 :强行要把一个一个的字典放到文件中,再一个一个取出来?

# dic = {'key1' : 'value1','key2' : 'value2'}

# with open('json_file','w') as f:

# str_dic = json.dumps(dic)

# f.write(str_dic+'\n')

# str_dic = json.dumps(dic)

# f.write(str_dic + '\n')

# str_dic = json.dumps(dic)

# f.write(str_dic + '\n')

# with open('json_file','r') as f:

# for line in f:

# dic = json.loads(line.strip())

# print(dic.keys())

json.dumps(),各个参数含义

Skipkeys:默认值是False,如果dict的keys内的数据不是python的基本类型(str,unicode,int,long,float,bool,None),设置为False时,就会报TypeError的错误。此时设置成True,则会跳过这类key

ensure_ascii:,当它为True的时候,所有非ASCII码字符显示为\uXXXX序列,只需在dump时将ensure_ascii设置为False即可,此时存入json的中文即可正常显示。)

Iindent:应该是一个非负的整型,如果是0就是顶格分行显示,如果为空就是一行最紧凑显示,否则会换行且按照indent的数值显示前面的空白分行显示,这样打印出来的json数据也叫pretty-printed json

separators:分隔符,实际上是(item_separator, dict_separator)的一个元组,默认的就是(‘,’,’:’);这表示dictionary内keys之间用“,”隔开,而KEY和value之间用“:”隔开。

sort_keys:将数据根据keys的值进行排序。

import json

data = {'username':['呜呜呜','奋斗奋斗'],'sex':'m','age':24}

json_dic = json.dumps(data,sort_keys=True,indent=4,separators=(',',':'),ensure_ascii=False)

print(json_dic)

1290440-20190923202221503-1795536100.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值