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

您的位置:首頁技術(shù)文章
文章詳情頁

python將字典內(nèi)容寫入json文件的實(shí)例代碼

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

python將字典內(nèi)容寫入json文件的方法:我們可以先使用json.dumps()函數(shù)將字典轉(zhuǎn)換為字符串;然后再將內(nèi)容寫入json即可。json.dumps()函數(shù)負(fù)責(zé)對(duì)數(shù)據(jù)進(jìn)行編碼。

字典內(nèi)容寫入json時(shí),需要用json.dumps將字典轉(zhuǎn)換為字符串,然后再寫入。

json也支持格式,通過參數(shù)indent可以設(shè)置縮進(jìn),如果不設(shè)置的話,則保存下來會(huì)是一行。

舉例:

無縮進(jìn):

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)

有縮進(jìn):

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將字典內(nèi)容寫入json文件的實(shí)例代碼

以上就是python將字典內(nèi)容寫入json文件的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于python如何將字典內(nèi)容寫入json文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 榆社县| 屏南县| 新干县| 靖西县| 渑池县| 尚志市| 襄城县| 鹤庆县| 历史| 永兴县| 绵竹市| 龙海市| 白山市| 潮州市| 宁安市| 陇南市| 桐乡市| 喀喇沁旗| 方城县| 宜丰县| 泸溪县| 汉寿县| 伊川县| 凌云县| 新建县| 宁晋县| 澄城县| 安泽县| 安西县| 惠来县| 阿巴嘎旗| 泸州市| 固阳县| 新化县| 凤庆县| 镇宁| 浙江省| 阳原县| 广昌县| 辉县市| 浙江省|