成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

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

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

瀏覽:72日期: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()

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产片91 | 颜值超高的女神啪啪 | 玖玖在线国产精品 | 日本欧美做爰全免费的视频 | 一级一级一片免费高清 | 国产乱淫视频 | 成人午夜在线观看 | 中文字幕在线视频网站 | 中国老太性色xxxxxhd | 久久最新免费视频 | 久久成人18 | 国内欧美一区二区三区 | 日韩中文精品亚洲第三区 | 亚洲视频在线观看 | 久久综合狠狠综合久久综合88 | 欧美高清一区 | 91精品国产薄丝高跟在线看 | 国产成人18黄网站免费 | 福利岛国深夜在线 | 中文字幕亚洲高清综合 | 久久亚洲国产成人影院 | 国产黄三级三·级三级 | 真人一级毛片全部免 | 欧美成人交tv免费观看 | 欧美 自拍 丝袜 亚洲 | 国产人成 | 成人福利网站在线看视频 | 亚洲欧美日产综合在线看 | 久久福利精品 | 欧美69xx| 国产精品一区二区丝瓜 | 日韩理论视频 | 久久国产网站 | 在线播放高清国语自产拍免费 | 99看视频| 免费一级毛片在线播放视频 | 成人a大片高清在线观看 | 亚洲香蕉影院 | 久久黄色精品视频 | 欧美又粗又硬又大久久久 | 国产高清免费影视在线观看 |