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

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

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

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

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

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

json也支持格式,通過參數(shù)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將字典內(nèi)容寫入json文件的實例代碼

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 胶州市| 都匀市| 马山县| 惠来县| 吴川市| 永登县| 杭锦旗| 嫩江县| 三都| 凉城县| 定边县| 庆城县| 玉树县| 岳阳市| 若羌县| 平昌县| 德保县| 军事| 四川省| 丹东市| 云安县| 延安市| 开化县| 鄢陵县| 澄迈县| 巴塘县| 隆昌县| 扎囊县| 田林县| 太和县| 乐安县| 全南县| 内丘县| 酉阳| 中卫市| 柳江县| 日土县| 芷江| 承德市| 金乡县| 平山县|