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數據類型的映射

Python JSON 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)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM