常常遇到複雜嵌套字典數據,咱們都是這麼寫的python
data = {'a': {'b': {'c': 'd'}}} print(data['a']['b']['c']) 'd'
而後常常遇到這個buggit
data2 = {'a': {'b': None}} print(data2['a']['b']['c']) Traceback (most recent call last): ... TypeError: 'NoneType' object is not subscriptable
有沒有以爲['a']['b']['c']這樣寫既麻煩,還不靠譜。glom的出現能夠完美的解決這個問題。github
glom地址 https://github.com/mahmoud/glomide
Python's nested data operator (and CLI), for all your declarative restructuring needs.rest
python嵌套數據操做庫,知足您全部的數據重構須要。code
1、glom.glom
glom.glom(target, spec,kwargs)ip
target:你傳入的字典數據get
spec:關鍵詞路徑。至關於過去的['a']['b']['c'],可是在glom中,路徑的寫法簡化爲'a.b.c'it
default:若是字典中沒有關鍵詞,那麼返回默認default的值。io
具體代碼
from glom import glom data = {'a': {'b': {'c': 'd'}}} print(glom(data, 'a.b.c')) #data中沒有d這個key,設置default後就不會保存 print(glom(data, 'a.b.c.d', default='錯了')) #沒有設置default,報錯 print(glom(data, 'a.b.c.d')) d 錯了 raise PathAccessError(e, parts, i) glom.core.PathAccessError: could not access 'd', index 3 in path Path('a', 'b', 'c', 'd'), got error: AttributeError("'str' object has no attribute 'd'",)
2、glom.Path
上面關鍵詞順序'a.b.c'在大部分都是能夠用的。可是有時候字典中的key是帶有 '.'的,或者key是整數數字,剛剛的方法會有問題。
target = {'a': {'b': 'c', 'd.e': 'f', 2: 3}} print(glom(target, 'a.2')) print(glom(target, 'a.d.e')) KeyError: '2' During handling of the above exception, another exception occurred: glom.core.PathAccessError: could not access '2', index 1 in path Path('a', '2'), got error: KeyError('2',)
這時候咱們須要使用Path來處理這些狀況。
from glom import glom, Path target = {'a': {'b': 'c', 'd.e': 'f', 2: 3}} print(glom(target, Path('a', 2))) print(glom(target, Path('a', 'd.e'))) 3 f
3、glom.Literal
有時候咱們須要對字典數據進行替換操做。 如
target = {'a': {'b': 'c'}} target['a'] = target['a']['b'] target['readability'] = 'counts' print(target) {'a': 'c', 'readability': 'counts'}
而在glom中,是這樣實現的。Literal告訴glom.glom我此次傳入的是value,而不是字典的key。
target = {'a': {'b': 'c'}} spec = {'a': 'a.b', 'readability': Literal('counts')} print(glom(target, spec)) {'a': 'c', 'readability': 'counts'}
4、glom.Coalesce
有時候咱們不知道字典中有哪些關鍵詞,這時候可能要一個個的試驗。在glom中提供了Coalesce,能夠給glom.glom我傳入的是多個key,你都給在字典裏找找,找到了告訴我結果。
target = {'c': 'd'} print(glom(target, Coalesce('a', 'b', 'c'))) d
假如字典中一個都沒找到,會引發程序報錯。因此咱們要設置default參數。
target = {} print(glom(target, Coalesce('a', 'b', 'c'), default='d-fault')) d-fault
5、glom.OMIT
OMIT意思是移除。在glom.glom中的spec參數這裏咱們能夠傳入含有lambda表達式的字典。
下面代碼中的t指代target字典自己。若是target['a']=='a',那麼target不變。
若是target['a']!=='a',那麼OMIT,即移除。
target = {'a': 'b'} spec = {'a': lambda t: t['a'] if t['a'] == 'a' else OMIT} print(glom(target, spec)) {} target = {'a': 'a'} print(glom(target, spec)) {'a': 'b'}