久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

瀏覽:2日期: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 編程
相關文章:
主站蜘蛛池模板: 德昌县| 固原市| 科技| 左贡县| 常德市| 青阳县| 兴国县| 鄂温| 新邵县| 吉木萨尔县| 凌云县| 陇川县| 潮州市| 黔江区| 文登市| 如东县| 木里| 岳普湖县| 渝北区| 望城县| 拉萨市| 南雄市| 枞阳县| 通州区| 城市| 买车| 林西县| 玉溪市| 香格里拉县| 交口县| 兴山县| 双江| 乌审旗| 灌阳县| 方城县| 白城市| 镇宁| 竹北市| 长汀县| 义乌市| 利川市|