欢迎来到李先生的博客

深山的鹿,不知归处;万般皆苦,只可自渡。
扩大
缩小

Python中json.dump()和json.dumps()的区别

一、图解

json.dumps(dict, indent):将Python对象转换成json字符串
json.dump(dict, file_pointer):将Python对象写入json文件

 

 

二、json.dumps()用法

1、用法

json.dumps(dict, indent):将Python对象转换成json字符串

2、参数
dict:被转换的名称
indent:打印格式的参数

3、例子

import json

dictionary ={
"id": "04",
"name": "sunil",
"depatment": "HR"
}

json_object = json.dumps(dictionary, indent=4)
print(json_object)


2、Python和Json数据类型的映射

PythonJSON Equivalent
dict object
list, tuple array
str string
int, float number
True true
False false
None null

 

三、json.dumps()用法

1、用法

json.dump(dict, file_pointer):将Python对象写入json文件

2、参数
dict:被转换的名称
file_pointer:打开文件的指针

3、例子

import json

dictionary = {
"id": "04",
"name": "sunil",
"depatment": "HR"
}

with open("jsons_txt", "w") as filedata:
json.dump(dictionary, filedata)

 

四、写在最后

 李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。公众号:运维汪(ID:Leeeee_Li)。

 

posted on 2021-05-26 11:01  Captain_Li  阅读(1004)  评论(0编辑  收藏  举报

导航