網站首頁 編程語言 正文
easydict是什么
用一句話來說就是,讓操作字典像是操作類成員方式一樣方便。
這個工具其實沒有很多要說的,因為它太簡單了,簡單到網上隨便搜搜就能用了,我從發(fā)現(xiàn)到使用只花了一秒鐘,嗯,夸張一下。
一、介紹
在 Python 中當我們需要訪問字典中的元素的時候,我們需要使用類似 a['example']
的形式來進行使用。例如現(xiàn)在我們有如下的字典
d = {'foo':3, 'bar':{'x':1, 'y':2}} print(d['foo']) # 如何想要訪問字典的元素需要這么寫 print(d['bar']['y']) # 如果想要繼續(xù)訪問字典中字典的元素需要使用二維數(shù)組
現(xiàn)在我們希望可以使用類似訪問屬性的方式
,來訪問字典里的變量,例如使用 d.foo
這種形式來訪問。這個時候就可以使用 easydict
這個模塊了。
二、安裝
EasyDict允許訪問dict值作為屬性(遞歸工作)。也就是可以方便地應用 .
來訪問dict的值。
pip install easydict
三、使用
示例
from easydict import EasyDict test = {'foo':3, 'bar':{'x':1, 'y':2}} e_dict = EasyDict(test) print(e_dict.foo) # 3 print(e_dict.bar.x) # 1
新增元素
from easydict import EasyDict a = EasyDict() # 新建空的dict a.x = 1 #新增元素 a.y = 2 a.z = 'new' print(a) # a: {'x':1, 'y':2, 'z':'new'}
update()函數(shù)把字典dict2的鍵/值對更新到dict里面。 意思就是 把一個字典的鍵值對 更新到 另一個字典里。 1. 實例: dict = {'Name": 'Zara', 'Age':7} dict2 ={ 'Sex': 'female' } dict.update(dict2) print "Value: %s" % dict 輸出: Value: {'Age': 7, 'Name': 'Zara', 'Sex': 'female' } 2. 注意:有相同的鍵會直接替換成update的值: a = {1: 2, 2: 2} b = {1: 1, 3: 3} b.update(a) print (b) 輸出: {1: 2, 2: 2, 3: 3} 3. 在已有的dict中增加新元素 a = {'x':1, 'y':2, 'z':3} a.update(EasyDict(version = 'v1')) print(a) # {'x':1, 'y':2, 'z':3, 'version':'v1'} 返回值: 該方法沒有任何返回值。
- easydict轉換為普通的dict
有的時候我們需要將 easydict 轉換為普通的 dict(例如使用 yaml.dump 進行保存的時候)。我們可以直接使用 dict
進行類型轉換(推薦這一種方式),如下所示:
from easydict import EasyDict as ed a = {'x':1, 'y':2, 'z':3} e_dict = ed(a) print(type(e_dict)) # easydict.EasyDict e_dict = dict(e_dict) print(type(e_dict)) # dict
除了上面的方式之外,我們可以使用 json,首先利用 json.dumps
將 dict 轉為字符串,接著使用 json.loads
將字符串轉為 dict(方法二,沒有上面的方式簡潔)。
處理 json/yaml 里的信息
import json from easydict import EasyDict as Edict with open(json_path_ori, 'r') as json_cache: data = Edict(json.load(json_cache)) return data
import yaml from easydict import EasyDict def setup_config(): with open(os.path.join('test_config.yaml')) as f: cfg = yaml.safe_load(f) # 讀取配置文件 cfg = EasyDict(cfg) # 存成 Easydict 的格式 return cfg
原文鏈接:https://blog.csdn.net/All_In_gzx_cc/article/details/127732706
相關推薦
- 2022-05-22 ansible管理工具的環(huán)境及部署安裝_服務器其它
- 2023-06-13 Python的加密模塊之hashlib?與?base64詳解及常用加密方法_python
- 2022-07-08 .NET?core項目AsyncLocal在鏈路追蹤中的應用_實用技巧
- 2022-06-06 解決http://localhost:3000/favicon.ico 的404 問題(含案例解析)
- 2024-02-26 Cannot execute binary file 之原因
- 2022-08-10 C#對文件名智能排序的算法_C#教程
- 2022-06-16 Go基礎教程系列之回調函數(shù)和閉包詳解_Golang
- 2022-04-22 Golang執(zhí)行流程詳解,兩種執(zhí)行流程方式有什么不同
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支