1. 程式人生 > >yaml + easydict 寫 配置檔案

yaml + easydict 寫 配置檔案

yaml

test.yaml檔案:

name: Tom Smith
age: 37
spouse:
    name: Jane Smith
    age: 25
children:
 - name: Jimmy Smith
   age: 15
 - name1: Jenny Smith
   age1: 12

python檔案:

import yaml,os
 
#獲取檔案全路徑
filename = os.path.join(os.path.dirname(__file__),'test.yaml').replace("\\","/")
 
f = open
(filename) y = yaml.load(f) print s

輸出:

{'age': 37, 'spouse': {'age': 25, 'name': 'Jane Smith'}, 'name': 'Tom Smith', 'children': [{'age': 15, 'name': 'Jimmy Smith'}, {'age1': 12, 'name1': 'Jenny Smith'}]}

easydict

easydict的作用:可以使得以屬性的方式去訪問字典的值

>>> from easydict import EasyDict as
edict >>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}}) >>> d.foo 3 >>> d.bar.x 1 >>> d = edict(foo=3) >>> d.foo 3

yaml+ easydict -> CNN config

參考OICR程式碼here

References

[1]Python讀取Yaml檔案
[2]python 中easydict的簡單使用