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

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

python字典與json轉(zhuǎn)換的方法總結(jié)

瀏覽:61日期:2022-07-01 09:07:09

在python中json分別由列表和字典組成,本文主要介紹python中字典與json相互轉(zhuǎn)換的方法。使用json.dumps可以把字典轉(zhuǎn)成json字符串。使用json.loads可以把json字符串轉(zhuǎn)為字典類型的數(shù)據(jù)。

1、字典轉(zhuǎn)json

使用json.dumps

json.dumps是對python對象編碼成json對象,可以把字典轉(zhuǎn)成json字符串。

方法格式

#字典轉(zhuǎn)換成json字符串 json.dumps(dict)

實例

# 創(chuàng)建字典info_dict = {’name’: ’Joe’, ’age’: 20, ’job’: ’driver’}# dumps 將數(shù)據(jù)轉(zhuǎn)換成字符串info_json = json.dumps(info_dict,sort_keys=False, indent=4, separators=(’,’, ’: ’))# 顯示數(shù)據(jù)類型print(type(info_json))f = open(’info.json’, ’w’)f.write(info_json)2、json轉(zhuǎn)字典

使用json.loads

json.loads是將json對象解碼成python對象,即用于將字典類型的數(shù)據(jù)轉(zhuǎn)成json字符串。

方法格式

#json字符串轉(zhuǎn)換成字典json.loads(json_str)

使用實例

In [25]: j Out[25]: ’{'name': 'mary', 'age': 21}’ In [26]: result = json.loads(j) In [27]: result Out[27]: {’name’: ’mary’, ’age’: 21} In [28]: type(result) Out[28]: dict

python字典和json字符串相互轉(zhuǎn)化的實例擴展

import json'''dumps:將python中的字典轉(zhuǎn)換為字符串output:{’fontFamily’: ’微軟雅黑’, ’fontSize’: 12, ’BaseSettings’: {’font’: 1, ’size’: {’length’: 40, ’wigth’: 30}}}{'fontFamily': 'u5faeu8f6fu96c5u9ed1', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}'''def json_dumps():json_dict = {’fontFamily’: ’微軟雅黑’, ’fontSize’: 12, ’BaseSettings’: {’font’: 1, ’size’: {’length’: 40, ’wigth’: 30}}}print(type(json_dict))print(json_dict)json_str = json.dumps(json_dict)print(type(json_str))print(json_str)'''dump:將數(shù)據(jù)寫入json文件中'''def json_dump():json_dict = {’fontFamily’: ’微軟雅黑’, ’fontSize’: 12, ’BaseSettings’: {’font’: 1, ’size’: {’length’: 40, ’wigth’: 30}}}with open('../file/record.json', 'w')as f:json.dump(json_dict, f)print('finished')'''loads:將字符串轉(zhuǎn)換為字典output:{'fontFamily': '微軟雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}{’fontFamily’: ’微軟雅黑’, ’fontSize’: 12, ’BaseSettings’: {’font’: 1, ’size’: {’length’: 40, ’wigth’: 30}}}'''def json_loads():json_str = ’{'fontFamily': 'u5faeu8f6fu96c5u9ed1', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}}’print(type(json_str))print(json_str)json_dict = json.loads(json_str)print(type(json_dict))print(json_dict)'''load:讀文件,并把字符串變換為Python數(shù)據(jù)類型output:40{’fontFamily’: ’微軟雅黑’, ’fontSize’: 12, ’BaseSettings’: {’font’: 1, ’size’: {’length’: 40, ’wigth’: 30}}}'''def json_load():f = open('../file/record.json', encoding=’utf-8’)setting = json.load(f)print(setting[’BaseSettings’][’size’][’length’])setting[’BaseSettings’][’size’][’length’] = 40print(setting)if __name__ == ’__main__’:json_dumps()json_dump()json_loads()json_load()

到此這篇關(guān)于python字典與json轉(zhuǎn)換的方法總結(jié)的文章就介紹到這了,更多相關(guān)python中字典與json相互轉(zhuǎn)換的方法內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 韶山市| 柳林县| 贵德县| 胶州市| 苍梧县| 衡水市| 刚察县| 紫金县| 寻乌县| 三台县| 旅游| 金寨县| 抚松县| 香港| 同德县| 遂平县| 宣城市| 迭部县| 九龙城区| 楚雄市| 嘉义县| 嘉善县| 将乐县| 溆浦县| 新乡市| 威海市| 长岛县| 德兴市| 茂名市| 贡觉县| 阿拉尔市| 鹤山市| 平阳县| 黑龙江省| 大名县| 余干县| 广德县| 寿宁县| 章丘市| 长葛市| 临江市|