一、圖解
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)。
