Python-json模块

一、相关概念

序列号 和反序列号

序列号:把内存中的数据类型转成一种特定格式,这种格式(json/pickle)可以用于存储,或者传输给其他平台

import json

内存中是数据类型 ----> 序列化 ----> 特定格式(json/pickle)

内存中是数据类型 <---- 反序列化 <---- 特定格式(json/pickle)

用途:

1) 保存程序运行状态(存档)

2)跨平台数据交互(只能交互共有的数据类型),例:Java --- Python,只能用json,pickle是Python专用格式

二、json模块

json 是一种通用格式,只能序列化所有编程语言都支持的数据类型;

1、序列化

(1)第一种方式

复制代码
dic = {'name': '李元芳', 'age': 27, 'salary': 30, 'married': False, 'hobbies': '大人此事必有蹊跷'}
json_res = json.dumps(dic)
json_res1 = json.dumps(dic,ensure_ascii=False) # 确保所有字符能够用ASCII码表示
with open('data/test.json',mode='wt',encoding='utf-8') as f:
    f.write(json_res)

print(json_res, type(json_res))
print(json_res1, type(json_res1))

运行结果:

{"name": "\u674e\u5143\u82b3", "age": 27, "salary": 30, "married": false, "hobbies": "\u5927\u4eba\u6b64\u4e8b\u5fc5\u6709\u8e4a\u8df7"} <class 'str'>

{"name": "李元芳", "age": 27, "salary": 30, "married": false, "hobbies": "大人此事必有蹊跷"} <class 'str'>

{"name": "\u674e\u5143\u82b3";双引号是所有编程语言通用的语法,汉字编程unicode格式二进制,这个可以控制

(2)第二种方式

复制代码
dic = {'name': '李元芳', 'age': 27, 'salary': 30, 'married': False, 'hobbies': '大人此事必有蹊跷'}
with open('data/test.json', mode='wt', encoding='utf-8') as f:
    json.dump(dic, f,ensure_ascii= False)  # 将序列化对象传给 f

2、 反序列化

(1)第一种方式

复制代码
dic = json.loads('{"name": "李元芳", "age": 27, "salary": 30, "married": false, "hobbies": "大人此事必有蹊跷"}')
print(dic, type(dic))
复制代码
# 从json文件中取值
with open('data/test.json',mode='rt',encoding='utf-8') as f:
    json_res = f.read()
dic = json.loads(json_res)
print(dic, type(dic))

运行结果:

{'name': '李元芳', 'age': 27, 'salary': 30, 'married': False, 'hobbies': '大人此事必有蹊跷'} <class 'dict'>

(2)第二种方式

复制代码
with open('data/test.json',mode='rt',encoding='utf-8') as f:
    json_res = json.load(f)
print(json_res)

注:

复制代码
json.dumps({1,2,3})

TypeError: Object of type set is not JSON serializable


三、pickle模块

复制代码
dic = {
    'name': '李元芳',
    'age': 27,
    'salary': 30,
    'married': False,
    'hobbies': ['大人此事必有蹊跷', '如燕', '果然不出大人所料'],
    's': {1, 2, 3, 4, 5, 7}
}
复制代码
pickle_res = pickle.dumps(dic)
print(pickle_res,type(pickle_res))

运行结果:

b'\x80\x04\x95\x97\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04name\x94\x8c\t\xe6\x9d\x8e\xe5\x85\x83\xe8\x8a\xb3\x94\x8c\x03age\x94K\x1b\x8c\x06salary\x94K\x1e\x8c\x07married\x94\x89\x8c\x07hobbies\x94]\x94(\x8c\x18\xe5\xa4\xa7\xe4\xba\xba\xe6\xad\xa4\xe4\xba\x8b\xe5\xbf\x85\xe6\x9c\x89\xe8\xb9\x8a\xe8\xb7\xb7\x94\x8c\x06\xe5\xa6\x82\xe7\x87\x95\x94\x8c\x18\xe6\x9e\x9c\xe7\x84\xb6\xe4\xb8\x8d\xe5\x87\xba\xe5\xa4\xa7\xe4\xba\xba\xe6\x89\x80\xe6\x96\x99\x94e\x8c\x01s\x94\x8f\x94(K\x01K\x02K\x03K\x04K\x05K\x07\x90u.' <class 'bytes'>

复制代码
pickle_res = pickle.dumps(dic,protocol=0)  # 防止乱码

with open('data/test.picle',mode='wb') as f:
    f.write(pickle_res)

运行结果:

复制代码
with open('data/test1.picle',mode='wb') as f:
    pickle.dump(dic,f,protocol=0)
相关推荐
蒋星熠5 小时前
实证分析:数据驱动决策的技术实践指南
大数据·python·数据挖掘·数据分析·需求分析
独行soc6 小时前
2025年渗透测试面试题总结-250(题目+回答)
网络·驱动开发·python·安全·web安全·渗透测试·安全狮
csdn_wuwt6 小时前
前后端中Dto是什么意思?
开发语言·网络·后端·安全·前端框架·开发
print(未来)6 小时前
C++ 与 C# 的性能比较:选择合适的语言进行高效开发
java·开发语言
四问四不知6 小时前
Rust语言入门
开发语言·rust
JosieBook6 小时前
【Rust】 基于Rust 从零构建一个本地 RSS 阅读器
开发语言·后端·rust
云边有个稻草人6 小时前
部分移动(Partial Move)的使用场景:Rust 所有权拆分的精细化实践
开发语言·算法·rust
一晌小贪欢7 小时前
Pandas操作Excel使用手册大全:从基础到精通
开发语言·python·自动化·excel·pandas·办公自动化·python办公
松涛和鸣8 小时前
11.C 语言学习:递归、宏定义、预处理、汉诺塔、Fibonacci 等
linux·c语言·开发语言·学习·算法·排序算法
IT痴者9 小时前
《PerfettoSQL 的通用查询模板》---Android-trace
android·开发语言·python