day22_3-json模块
# 参考资料:
# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园
# https://www.cnblogs.com/guojintao/articles/9070485.html
# ------------------------------------------------------------
# ********************day22_3-json模块 *******************
# ********************day22_3-json模块 *******************
# ********************day22_3-json模块 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# =====>>>>>>内容概览 # ------------------------------------------------------------
# # 0、json概述
# # # JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。
# # # Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
# # # json.dumps(): 对数据进行编码。
# # # json.loads(): 对数据进行解码。
# # # Python3 JSON 数据解析 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-json.html
# # # 作用: 可以实现跨平台实现数据的传输,不不仅仅是在python之间,可以在多个语言平台之间实现数据传输
# # # 也因此,在数据传输的编码与解码上,经常通过json来进行,而pickle的使用率会低下一些
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 1、不用jason方法实现在写入文件的数据进行还原
# # # 使用json方法见4
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 2、json.dumps()
# # # json.dumps(): 对数据进行编码。
# # # 需要注意的是:json的dumps会数据进行统一化处理,所有的数据-->双引号的字符串
# # # 如 s = 'hello ---->"hello"------>'"hello"'
# # # 注意与json.dump()区别,json.dump()将转化的数据通过json内置处理直接写入到文件中,
# # # 而dumps操作是先将数据转化为json数据,在通过文件的处理方法,之后再再将数据写入到文件中,
# # # 使用上,json.dumps()为常用
# # # json.dump()----->>>见下面实例???
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 3、json.loads()
# # # json.loads(): 对数据进行解码。
# # # 实现对数据从json格式中,还原为原来的格式
# # # 注意与json.load()区别,使用上,json.loads()为常用,具体的与上面的序列2的类似
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 4、json.dumps与json.loads 在文件传输中的应用
# # # 通过jason方法,实现在写入文件的数据进行还原
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 5、json.dump与json.load 在文件传输中的应用
# # # 这个与dumps、loads的区别就,是这个是直接读取文件的内容并实现json转码,步聚是合在一起的
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 6、json.dumps与json.loads 的一些补充
# # # json.dumps与json.loads 不一定需要搭配使用,只要他们文件中保存的数据满足json读取到的
# # # 格式就是可了
# # # 即,文件储存的时候的编码不一定需要使用json.dumps来编码,只要格式满足json格式,读取
# # # 一样可以通过json.loads来解码读取
# ------------------------------------------------------------
''' '''
# ------------------------------------------------------------
# # 0、json概述
# # # JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。
# # # Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
# # # json.dumps(): 对数据进行编码。
# # # json.loads(): 对数据进行解码。
# # # Python3 JSON 数据解析 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-json.html
# # # 作用: 可以实现跨平台实现数据的传输,不不仅仅是在python之间,可以在多个语言平台之间实现数据传输
# # # 也因此,在数据传输的编码与解码上,经常通过json来进行,而pickle的使用率会低下一些
# ------------------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 1、不用jason方法实现在写入文件的数据进行还原
# # # 使用json方法见4
# ------------------------------------------------------------
'''
#
# # 写入到文件中
# print("写入部份".center(50,"-"))
# dic = {"name":"alex",
# 'sex':'male'}
# f = open("hello","w")
# # dic需要转化为字符串,才能存储到系统中
# dic_s = str(dic)
# print(dic_s,type(dic_s))
# f.write(dic_s)
#
# # 如果不关闭,刚刚写入的数据还放在内存中,还没有写进去,导致下面读取到的数据是“空”
# # f.close()
# # 如果不用上面的关闭操作,可以将在内存中的文件流通到flush操作刷入内存中,下面就可以读取到数据了
# f.flush()
#
# print("读取部份".center(50,"-"))
# # 将刚刚写入的数据读取出来
# f_read = open("hello","r")
# data = f_read.read()
# print(data,type(data))
#
# print("还原为字典".center(50,"-"))
# data = eval(data)
# print(data,type(data))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # -----------------------写入部份-----------------------
# # {'name': 'alex', 'sex': 'male'} <class 'str'>
# # -----------------------读取部份-----------------------
# # {'name': 'alex', 'sex': 'male'} <class 'str'>
# # ----------------------还原为字典-----------------------
# # {'name': 'alex', 'sex': 'male'} <class 'dict'>
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 2、json.dumps()
# # # json.dumps(): 对数据进行编码。
# # # 需要注意的是:json的dumps会数据进行统一化处理,所有的数据-->双引号的字符串
# # # 如 s = 'hello ---->"hello"------>'"hello"'
# # # 注意与json.dump()区别,json.dump()将转化的数据通过json内置处理直接写入到文件中,
# # # 而dumps操作是先将数据转化为json数据,在通过文件的处理方法,之后再再将数据写入到文件中,
# # # 使用上,json.dumps()为常用
# # # json.dump()----->>>见下面实例???
# ------------------------------------------------------------
'''
# import json
# dic = {"name":"alex",
# 'sex':'male'} # ---->{"name":"alex"}----->'{"name":"alex"}'
# i = 8 # ---->'8'
# s = 'hello' # ---->"hello"------>'"hello"'
# l = [11,22,"??"] # ---->"[11,22]"
#
# dic_jason = json.dumps(dic)
# i_jason = json.dumps(i)
# s_jason = json.dumps(s)
# l_jason = json.dumps(l)
# print("字典: ",dic_jason,type(dic_jason))
# print("整数: ",i_jason,type(i_jason))
# print("字符串:",s_jason,type(s_jason))
# print("列表: ",l_jason,type(l_jason))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # 字典: {"name": "alex", "sex": "male"} <class 'str'>
# # 整数: 8 <class 'str'>
# # 字符串: "hello" <class 'str'>
# # 列表: [11, 22, "??"] <class 'str'>
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 3、json.loads()
# # # json.loads(): 对数据进行解码。
# # # 实现对数据从json格式中,还原为原来的格式
# # # 注意与json.load()区别,使用上,json.loads()为常用,具体的与上面的序列2的类似
# ------------------------------------------------------------
'''
#
# import json
# dic = {"name":"alex",
# 'sex':'male'} # ---->{"name":"alex"}----->'{"name":"alex"}'
# i = 8 # ---->'8'
# s = 'hello' # ---->"hello"------>'"hello"'
# l = [11,22,"??"] # ---->"[11,22]"
#
# dic_jason = json.dumps(dic)
# i_jason = json.dumps(i)
# s_jason = json.dumps(s)
# l_jason = json.dumps(l)
#
# print("json.dumps数据编码部分".center(50,"-"))
# print("字典: ",dic_jason,type(dic_jason))
# print("整数: ",i_jason,type(i_jason))
# print("字符串:",s_jason,type(s_jason))
# print("列表: ",l_jason,type(l_jason))
#
# dic_loads = json.loads(dic_jason)
# i_loads = json.loads(i_jason)
# s_loads = json.loads(s_jason)
# l_loads = json.loads(l_jason)
#
# print("json.loads数据解码部分".center(50,"-"))
# print("字典: ",dic_loads,type(dic_loads))
# print("整数: ",i_loads,type(i_loads))
# print("字符串:",s_loads,type(s_loads))
# print("列表: ",l_loads,type(l_loads))
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # -----------------json.dumps数据编码部分-----------------
# # 字典: {"name": "alex", "sex": "male"} <class 'str'>
# # 整数: 8 <class 'str'>
# # 字符串: "hello" <class 'str'>
# # 列表: [11, 22, "??"] <class 'str'>
# # -----------------json.loads数据解码部分-----------------
# # 字典: {'name': 'alex', 'sex': 'male'} <class 'dict'>
# # 整数: 8 <class 'int'>
# # 字符串: hello <class 'str'>
# # 列表: [11, 22, '??'] <class 'list'>
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 4、json.dumps与json.loads 在文件传输中的应用
# # # 通过jason方法,实现在写入文件的数据进行还原
# ------------------------------------------------------------
'''
#
# import json
#
# # 写入到文件中
# print("写入部份".center(50,"-"))
# dic = {"name":"alex",
# 'sex':'male'}
# f = open("hello","w")
# # dic需要转化为字符串,才能存储到系统中 这里的编码与“上面序列1”中的区别
# dic_s = json.dumps(dic)
# print(dic_s,type(dic_s))
# f.write(dic_s)
#
# # 如果不关闭,刚刚写入的数据还放在内存中,还没有写进去,导致下面读取到的数据是“空”
# # f.close()
# # 如果不用上面的关闭操作,可以将在内存中的文件流通到flush操作刷入内存中,下面就可以读取到数据了
# f.flush()
#
# print("读取部份".center(50,"-"))
# # 将刚刚写入的数据读取出来
# f_read = open("hello","r")
# data = f_read.read()
# print(data,type(data))
#
# print("还原为字典".center(50,"-"))
# data = json.loads(data)
# print(data,type(data))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # -----------------------写入部份-----------------------
# # {"name": "alex", "sex": "male"} <class 'str'>
# # -----------------------读取部份-----------------------
# # {"name": "alex", "sex": "male"} <class 'str'>
# # ----------------------还原为字典-----------------------
# # {'name': 'alex', 'sex': 'male'} <class 'dict'>
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 5、json.dump与json.load 在文件传输中的应用
# # # 这个与dumps、loads的区别就,是这个是直接读取文件的内容并实现json转码,步聚是合在一起的
# ------------------------------------------------------------
'''
#
# import json
#
# print("写入部份".center(50,"-"))
# dic = {"name":"alex",
# 'sex':'male'}
# f = open("hello","w")
# # json编码并且写到文件内
# print(json.dump(dic,f),"写入完成" )
#
# # 如果不关闭,刚刚写入的数据还放在内存中,还没有写进去,导致下面读取到的数据是“空”
# # f.close()
# # 如果不用上面的关闭操作,可以将在内存中的文件流通到flush操作刷入内存中,下面就可以读取到数据了
# f.flush()
#
# print("还原为字典".center(50,"-"))
# f_read = open("hello","r")
# data = json.load(f_read)
# print(data,type(data))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # -----------------------写入部份-----------------------
# # None 写入完成
# #
# # ----------------------还原为字典-----------------------
# # {'name': 'alex', 'sex': 'male'} <class 'dict'>
# #
# # Process finished with exit code 0 # 06
# 06
# 06
'''
# ------------------------------------------------------------
# # 6、json.dumps与json.loads 的一些补充
# # # json.dumps与json.loads 不一定需要搭配使用,只要他们文件中保存的数据满足json读取到的
# # # 格式就是可了
# # # 即,文件储存的时候的编码不一定需要使用json.dumps来编码,只要格式满足json格式,读取
# # # 一样可以通过json.loads来解码读取
# ------------------------------------------------------------
'''
# import json
# with open ("a","r") as f:
# data = f.read()
# print(data,type(data))
# data = json.loads(data) # 文件中的内容满足json格式内容
# print(data["name"])
# print(data, type(data))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_3-json.py
# # {"name":"alvin"} <class 'str'>
# # alvin
# # {'name': 'alvin'} <class 'dict'>
# #
# # Process finished with exit code 0
day22_3-json模块的更多相关文章
- python的json模块
Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. 环境配置 在使用 Python 编码或解码 JSON 数据前,我们需要先安装 JSON 模块. ...
- php安装json模块
centOS上因为看php源码中没有json模块,于是采用pecl自动编译安装:# yum install php-devel# yum install php-pear# yum install g ...
- 解决python中json模块loads出来的结构都是unicode的问题
在使用python的json模块对json字串反序列化成python对象的时候出现的字符串都是unicode类型,而不是python内置的str类型.在某种使用场景下用户必须做显式的转换才能正常使用, ...
- 全局变量 urllib模块 json模块
1.vars() 查看一个.py文件中的全局变量 print(vars()) #重点 __name__': '__main__ '__file__': 'C:/Users/lenovo/Pychar ...
- 我为什么要再给lua写一个json模块
最近要给自己编写的服务器加上json解析模块.根据我当前的项目,可以预测服务器中使用json的地方: 通信.由于与客户端通信使用google protocolbuffer,仅在与SDK通信中使用jso ...
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数
python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...
- perl json模块
JSON - JSON (JavaScript Object Notation) encoder/decoder 简介: use JSON; # imports encode_json, decode ...
- pickle和json模块
json模块 json模块是实现序列化和反序列化的,主要用户不同程序之间的数据交换,首先来看一下: dumps()序列化 import json '''json模块是实现序列化和反序列话功能的''' ...
- json模块和pickle模块的用法
在python中,可以使用pickle和json两个模块对数据进行序列化操作 其中: json可以用于字符串或者字典等与python数据类型之间的序列化与反序列化操作 pickle可以用于python ...
- 【python标准库模块四】Json模块和Pickle模块学习
Json模块 原来有个eval函数能能够从字符串中提取出对应的数据类型,比如"{"name":"zhangsan"}",可以提取出一个字典. ...
随机推荐
- php漂亮的分页类
<?php /* * PHP分页类 * @package Page * @Created 2013-03-27 * @Modify 2013-03-27 * ...
- javascript中的select、checkbox
遍历checkbox <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- Python正则表达式如何进行字符串替换实例
Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...
- git学习记录2(远程库管理)
学习参考地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 本编随笔只是自己对 ...
- 2019 牛客多校第三场 B Crazy Binary String
题目链接:https://ac.nowcoder.com/acm/contest/883/B 题目大意 给定一个长度为 N 的 01 字符串,输出最长子串和子序列的长度,满足其中 0 和 1 的个 ...
- CentOS7 相关配置
nginx 1.在线安装nginx yum install nginx 2.启动nginx服务 systemctl start nginx 3.防火墙设置 打开http防火墙:firewall-cmd ...
- this 关键字的使用及说明
this 是Java 中常见的一个关键字,它的主要作用是引用类的当前实例,本篇文章主要介绍 this 关键字的几种使用情况. 1. this 调用当前类的变量,也就是类中的成员变量. 代码示例: pu ...
- AT指令集之Call
1.//unsolicited result code,URC表示BP->AP+ESIPCPI:<call_id>,<dir>,<sip_msg_type>, ...
- Spring AOP源码分析(三):基于JDK动态代理和CGLIB创建代理对象的实现原理
AOP代理对象的创建 AOP相关的代理对象的创建主要在applyBeanPostProcessorsBeforeInstantiation方法实现: protected Object applyBea ...
- 《转》python 10 集合
自 http://www.cnblogs.com/BeginMan/p/3160565.html 一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除) ...