获取yaml中定义的或未定义的值的Python方法 - python

我有一个带有测试配置的yaml文件,并且在可选部分“ test-options”中有一个可选参数“ ignore-dup-txn”。

test-name:
    test-type:        trh_txn
    test-src-format:  excel
    test-src-excel-sheet:   invalid_txns
    test-options:
      ignore-dup-txn: True

我将“测试名称”部分阅读为“测试”字典,现在我以这种方式进行检查:

if 'test-options' in test and 'ignore-dup-txn' in test['test-options']:
    ignore_dups = test['test-options']['ignore-dup-txn']
else:
    ignore_dups = None

pythonic的方法是什么?更清晰,简单和短。

我当时想做“ getter”,但是如果我做get(test['test-option']['ignore-dup-txn'])的话,很明显,如果未定义option,我将得到一个例外。

python大神给出的解决方案

这将工作:

test.get('test-options', {}).get('ignore-dup-txn', None)