av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)Python教程:python如何將字典內(nèi)容寫(xiě)入json文件

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

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

(推薦教程:Python入門(mén)教程)

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

舉例:

無(wú)縮進(jìn):

from collections import defaultdict, OrderedDict
import json

video = 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, OrderedDict
import json

video = 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)

網(wǎng)頁(yè)標(biāo)題:創(chuàng)新互聯(lián)Python教程:python如何將字典內(nèi)容寫(xiě)入json文件
本文路徑:http://uogjgqi.cn/article/cochepc.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流