python字典與json轉(zhuǎn)換的方法總結(jié)
在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是對(duì)python對(duì)象編碼成json對(duì)象,可以把字典轉(zhuǎn)成json字符串。
方法格式
#字典轉(zhuǎn)換成json字符串 json.dumps(dict)
實(shí)例
# 創(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對(duì)象解碼成python對(duì)象,即用于將字典類型的數(shù)據(jù)轉(zhuǎn)成json字符串。
方法格式
#json字符串轉(zhuǎn)換成字典 json.loads(json_str)
使用實(shí)例
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)化的實(shí)例擴(kuò)展
import json """ dumps:將python中的字典轉(zhuǎn)換為字符串 output: {'fontFamily': '微軟雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} {"fontFamily": "\u5fae\u8f6f\u96c5\u9ed1", "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ù)寫(xiě)入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": "\u5fae\u8f6f\u96c5\u9ed1", "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'] = 40 print(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理
- 詳談js遍歷集合(Array,Map,Set)
- js實(shí)現(xiàn)復(fù)制功能(多種方法集合)
- jQuery實(shí)現(xiàn)簡(jiǎn)單復(fù)制json對(duì)象和json對(duì)象集合操作示例
- JS中的算法與數(shù)據(jù)結(jié)構(gòu)之集合(Set)實(shí)例詳解
- Redis緩存,泛型集合與json字符串的相互轉(zhuǎn)換實(shí)例
- python將字典內(nèi)容寫(xiě)入json文件的實(shí)例代碼
- python 如何將字典寫(xiě)為json文件
- JavaScript?數(shù)據(jù)結(jié)構(gòu)之字典方法
- JavaScript字典與集合詳解
相關(guān)文章
keras在構(gòu)建LSTM模型時(shí)對(duì)變長(zhǎng)序列的處理操作
這篇文章主要介紹了keras在構(gòu)建LSTM模型時(shí)對(duì)變長(zhǎng)序列的處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python 如何強(qiáng)制限定小數(shù)點(diǎn)位數(shù)
這篇文章主要介紹了Python 強(qiáng)制限定小數(shù)點(diǎn)位數(shù)的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03用Python編寫(xiě)簡(jiǎn)單的微博爬蟲(chóng)
這篇文章主要介紹了如何利用Python編寫(xiě)一個(gè)簡(jiǎn)單的微博爬蟲(chóng),感興趣的小伙伴們可以參考一下2016-03-03Python實(shí)現(xiàn)刪除列表中滿足一定條件的元素示例
這篇文章主要介紹了Python實(shí)現(xiàn)刪除列表中滿足一定條件的元素,結(jié)合具體實(shí)例形式對(duì)比分析了Python針對(duì)列表元素的遍歷、復(fù)制、刪除等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Python可視化學(xué)習(xí)之matplotlib內(nèi)置單顏色
這篇文章主要為大家介紹了Python matplotlib中支持的顏色格式及內(nèi)置的單顏色色號(hào)的匯總,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02pip安裝python庫(kù)時(shí)報(bào)Failed?building?wheel?for?xxx錯(cuò)誤的解決方法
最近在使用pip安裝python的時(shí)候遇到些問(wèn)題,所以下面這篇文章主要給大家介紹了關(guān)于pip安裝python庫(kù)時(shí)報(bào)Failed?building?wheel?for?xxx錯(cuò)誤的解決方法,需要的朋友可以參考下2023-01-01Python爬蟲(chóng)實(shí)戰(zhàn)演練之采集拉鉤網(wǎng)招聘信息數(shù)據(jù)
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python采集拉鉤網(wǎng)招聘信息數(shù)據(jù),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-10-10