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文档 ...
随机推荐
- Python+OpenCV图像处理(五)—— 像素运算
最近在忙毕业设计,只能偶尔更新博客........ 一.像素的算术运算 像素的算术运算涉及加减乘除等基本运算(要进行算术运算,两张图片的形状(shape)必须一样) 代码如下: #像素的算术运算(加. ...
- linux 查找locate find
1.locate locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在 执行loacte时直接找该索引,查询速度会较快 ...
- Golang的数组初始化方式及for-range遍历
package main import ( "fmt" ) func main() { ]], , } //定义并初始化长度为3的数组 fmt.Println(arr1) //[1 ...
- Python3 格式化字符串
Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...
- ssh-keygen 命令
功能 生成.管理和转换认证密钥,包括 RSA 和 DSA 两种密钥,密钥类型可以用 -t 选项指定.如果没有指定则默认生成用于SSH-2的RSA密钥,系统管理员还可以用它产生主机密钥. 通常,这个程序 ...
- python简说(五)操作文件
f = open('users.txt',encoding='utf-8') #读文件的时候,必须存在在才可以读 文件对象,或者文件句柄res = f.read()print(res)f.close( ...
- Codeforces 839A Arya and Bran
Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going ...
- 【python002-设计小游戏】
用python设计第一个游戏 一.运行idle的快捷方式:F5 二.首先设计一款小游戏: print('-------开启python学习之旅--------')temp = input(" ...
- jsoup对 HTML 文档的解析和操作
本文手动转载自http://www.cnblogs.com/chenying99/archive/2013/01/04/2844615.html,仅根据个人需要对实用部分进行转载,详细请阅读原文. j ...
- Installing Jenkins as a Windows service
Install Jenkins as a Windows service NOTE: if you installed Jenkins using the windows installer, you ...