国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python將字典內容寫入json文件的實例代碼

瀏覽:3日期:2022-07-14 13:35:34

python將字典內容寫入json文件的方法:我們可以先使用json.dumps()函數將字典轉換為字符串;然后再將內容寫入json即可。json.dumps()函數負責對數據進行編碼。

字典內容寫入json時,需要用json.dumps將字典轉換為字符串,然后再寫入。

json也支持格式,通過參數indent可以設置縮進,如果不設置的話,則保存下來會是一行。

舉例:

無縮進:

from collections import defaultdict, OrderedDictimport jsonvideo = defaultdict(list)video['label'].append('haha')video['data'].append(234)video['score'].append(0.3)video['label'].append('xixi')video['data'].append(123)video['score'].append(0.7)test_dict = { ’version’: '1.0', ’results’: video, ’explain’: {’used’: True,’details’: 'this is for josn test', }}json_str = json.dumps(test_dict)with open(’test_data.json’, ’w’) as json_file: json_file.write(json_str)

有縮進:

from collections import defaultdict, OrderedDictimport jsonvideo = defaultdict(list)video['label'].append('haha')video['data'].append(234)video['score'].append(0.3)video['label'].append('xixi')video['data'].append(123)video['score'].append(0.7)test_dict = { ’version’: '1.0', ’results’: video, ’explain’: {’used’: True,’details’: 'this is for josn test', }}json_str = json.dumps(test_dict, indent=4)with open(’test_data.json’, ’w’) as json_file: json_file.write(json_str)

python將字典內容寫入json文件的實例代碼

以上就是python將字典內容寫入json文件的實例代碼的詳細內容,更多關于python如何將字典內容寫入json文件的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 祁阳县| 福清市| 景宁| 文昌市| 临颍县| 邻水| 渝北区| 涪陵区| 夏河县| 谢通门县| 苍梧县| 孙吴县| 茌平县| 黔南| 称多县| 宜宾市| 镶黄旗| 南康市| 新丰县| 通江县| 蒲城县| 昌黎县| 阿克苏市| 昌乐县| 临邑县| 社旗县| 辉南县| 荥阳市| 车致| 黄骅市| 历史| 嘉鱼县| 平山县| 平邑县| 鹤岗市| 漠河县| 宁乡县| 新兴县| 江永县| 汉川市| 常山县|