python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。
1、json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
- json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
- json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)
在json的编解码过程中,python 的原始类型与json类型会相互转换,具体的转化对照如下:
Python 编码为 JSON 类型转换对应表:
| Python | JSON |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float, int- & float-derived Enums | number |
| True | true |
| False | false |
| None | null |
JSON 解码为 Python 类型转换对应表:
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
| true | True |
| false | False |
| null | None |
2、json.dump()和json.load()主要用来读写json文件函数
实例如下:
import json,time
# save data to json file
def store(data):
with open('data.json', 'w') as fw:
# 将字典转化为字符串
# json_str = json.dumps(data)
# fw.write(json_str)
# 上面两句等同于下面这句
json.dump(data,fw)
# load json data from file
def load():
with open('data.json','r') as f:
data = json.load(f)
return data if __name__ == "__main__":
json_data = '{"login":[{"username":"aa","password":"001"},{"username":"bb","password":"002"}],"register":[{"username":"cc","password":"003"},{"username":"dd","password":"004"}]}'
# 函数是将json格式数据转换为字典
data = json.loads(json_data)
store(data) data = load()
print(data)
输出
{'login': [{'username': 'aa', 'password': ''}, {'username': 'bb', 'password': ''}], 'register': [{'username': 'cc', 'password': ''}, {'username': 'dd', 'password': ''}]}
总结:
不带s的用于操作文件,带s的用于数据类型的转换。
python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)的更多相关文章
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- 使用Python读写csv文件的三种方法
Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...
- python读写csv文件
文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...
- 【python-ini】python读写ini文件
[python-ini]python读写ini文件 本文实例讲述了Python读写ini文件的方法.分享给大家供大家参考.具体如下: 比如有一个文件update.ini,里面有这些内容: 1 2 ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- Python读写Excel文件和正则表达式
Python 读写Excel文件 这里使用的是 xlwt 和 xlrd 这两个excel读写库. #_*_ coding:utf-8 _*_ #__author__='观海云不远' #__date__ ...
- (Python基础教程之十二)Python读写CSV文件
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
- python读写word、excel、csv、json文件
http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...
随机推荐
- Autel MaxiSys Pro MS908P Software Update Gudie
This article aims to guide on how to update software for Autel MaxiSys Pro. (Suitable for MaxiDiag E ...
- java.lang.IllegalStateException: Failed to check the status of the service
java.lang.IllegalStateException: Failed to check the status of the service com.pinyougou.sellergoods ...
- close yield
close的方法主要是关闭子生成器,需要注意的有4点: 1.如果生成器close后,还继续next,会报错StopIteration [图片] 2.如果我捕获了异常,将GeneratorE ...
- Java线程安全容器
一.Java同步容器 同步容器是用来解决并发情况下的容器线程安全问题的.给多线程环境准备一个线程安全的容器对象. 线程安全的容器对象: Vector, Hashtable.线程安全容器对象,都是使用s ...
- glibc 2.x release note
glibc 2.x release note,参见: https://sourceware.org/glibc/wiki/Glibc%20Timeline https://www.gnu.org/so ...
- The Usage of Lambda and Heap in the C++ STL
The Usage of Lambda and Heap in the C++ STL Heap In c++ STL, the heap had been implemented as the pr ...
- [J2EE]struts+ejb笔记
DispatchAtion: - org.apache.struts.actions.DispatchAction 这个类是个抽象类,但实现父类Action的execute方法,在项目中重写这个类可以 ...
- 计算多数的乘积(Python实现)
1 # -*- coding: utf-8 -*- # sum_of_products.py # @author 0yst3r # @description 两数之积及多数之积 # @created ...
- Oracle使用——Linux系统下使用命令实现oracle数据库数据导入
背景 在工作当中,数据库的备份及数据导入是必不可少的操作,在完全无界面的Linux操作系统中,我们应该怎样实现oracle数据库的导入呢 前提 服务器已配置ftp 模拟环境 一台linux应用服务器上 ...
- shell编程(四)之循环控制语句(for,while,until,break,continue,case)
for循环 语法:for NAME in LIST; do 循环体 done 列表生成方式:1.整数列表 {start...end} $(seq,[start [step]] end)2.glob / ...