# 参考资料:
# 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模块的更多相关文章

  1. python的json模块

    Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. 环境配置 在使用 Python 编码或解码 JSON 数据前,我们需要先安装 JSON 模块. ...

  2. php安装json模块

    centOS上因为看php源码中没有json模块,于是采用pecl自动编译安装:# yum install php-devel# yum install php-pear# yum install g ...

  3. 解决python中json模块loads出来的结构都是unicode的问题

    在使用python的json模块对json字串反序列化成python对象的时候出现的字符串都是unicode类型,而不是python内置的str类型.在某种使用场景下用户必须做显式的转换才能正常使用, ...

  4. 全局变量 urllib模块 json模块

    1.vars()  查看一个.py文件中的全局变量 print(vars()) #重点 __name__': '__main__ '__file__': 'C:/Users/lenovo/Pychar ...

  5. 我为什么要再给lua写一个json模块

    最近要给自己编写的服务器加上json解析模块.根据我当前的项目,可以预测服务器中使用json的地方: 通信.由于与客户端通信使用google protocolbuffer,仅在与SDK通信中使用jso ...

  6. Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数

    python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...

  7. perl json模块

    JSON - JSON (JavaScript Object Notation) encoder/decoder 简介: use JSON; # imports encode_json, decode ...

  8. pickle和json模块

    json模块 json模块是实现序列化和反序列化的,主要用户不同程序之间的数据交换,首先来看一下: dumps()序列化 import json '''json模块是实现序列化和反序列话功能的''' ...

  9. json模块和pickle模块的用法

    在python中,可以使用pickle和json两个模块对数据进行序列化操作 其中: json可以用于字符串或者字典等与python数据类型之间的序列化与反序列化操作 pickle可以用于python ...

  10. 【python标准库模块四】Json模块和Pickle模块学习

    Json模块 原来有个eval函数能能够从字符串中提取出对应的数据类型,比如"{"name":"zhangsan"}",可以提取出一个字典. ...

随机推荐

  1. vue footer点击变色

    <header class="tab_nav"> <div v-for="(item,index) in tabNav" @click=&qu ...

  2. Redis探索之路(六):Redis的常用命令

    一:键值相关命令 1.keys Pattern模糊查询 keys my* 2.exists某个key是否存在 exists key1 3.del 删除一个key del key1 4.expire设置 ...

  3. 二进制中1的个数(Java实现)

    问题: 输入一个整数,求其二进制中1的个数 看到这个问题,我们应该想到数的位运算: 解法一:我们每次将此数&1 ,如果结果等于1,证明此数的最后一位是1,,count++: 然后在将数右移一位 ...

  4. Spring容器对Bean组件的管理

    Bean对象创建 默认是随着容器创建 可以使用 lazy-init=true:在调用 getBean 延迟创建 也可以用 <beans default-lazy-init="true& ...

  5. Delphi 取整函数round、trunc、ceil和floor

    Delphi 取整函数round.trunc.ceil和floor 1.Round(四舍六入五留双)功能说明:对一个实数进行四舍五入.(按照银行家算法)例:var i, j: Integer;begi ...

  6. Tomcat 调优的技巧

    转载:www.cnblogs.com/wangsen, https://mp.weixin.qq.com/s/WrIsOOyR7o4SwSXMT0Zecg 最近,看到一篇讲述 Tomcat 调优的文章 ...

  7. NX二次开发-UFUN创建块UF_MODL_create_block

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); UF_FEATURE_SIGN Sign = ...

  8. NX二次开发-UFUN获取边的端点UF_MODL_ask_edge_verts

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> #include <uf ...

  9. NX二次开发-UF_MODL_ask_angle_tolerance获取建模的角度公差

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize() ...

  10. NX二次开发-UFUN编辑图层类别名字UF_LAYER_edit_category_name

    NX11+VS2013 #include <uf.h> #include <uf_layer.h> UF_initialize(); //创建图层类别 UF_LAYER_cat ...