System: ubuntu1404,python:2.7
Parse Yaml file to install Pyyaml First, you can directly use sudo pip install pyyaml install YAML module;
T.yaml file contents: 2 spaces or 4 spaces in a spacer
T.yaml "10L, 143C allhost:ip00:192.168.1.1 ip01:one:192.1 68.1.2 two:192.168.1.254soft:apache:2.2 mysql:5.2 php:5.3
3. Analysis:
in [160]: import yaml #倒入模块In [161]: s=yaml.load (file (' T.yaml ')) #加载t. Yaml configuration file in [162]: print s[' host ' [' ip00 '][:]192.168.1.1in [163]: print s[' host '] [' ip00 ']192.168.1.1in [164]: print s[' host ']{' ip01 ': {' both ': ' 192.168.1.254 ', ' one ': ' 192.168.1.2 '}, ' ip00 ': ' 192.168.1.1 '}in [165]: print s{' host ': {' ip01 ': {': ' 192.168.1.254 ', ' one ': ' 192.168.1.2 '}, ' ip00 ': ' 192.168.1.1 '}, ' Soft ': {' apache ': 2.2, ' php ': 5.3, ' MySQL ': 5.2}}in [166]: print s[' host ' [' ip00 ']192.168.1.1in [167]: print s[' host ' [' Ip01 ']{', ': ' 192.168.1.254 ', ' One ': ' 192.168.1.2 '}in [168]: print s[' host ' [' IP01 '] ' one ']192.168.1.2in [169 ]: print s[' soft ']{' APAChe ': 2.2, ' php ': 5.3, ' mysql ': 5.2}n [171]: print s[' soft ' [' Apache '] 2.2in [172]: print s[' soft ' [' php ']5.3
4. Write the Yaml configuration file:
File= ' Kkk.yaml ' data={' host ': {' ip01 ': {' one ': ' 192.168.1.254 ', ' One ': ' 192.168.1.2 '}, ' ip00 ': ' 192.168.1.1 '}, ' soft ': { ' Apache ': 2.2, ' php ': 5.3, ' MySQL ': 5.2}}f=open (file, ' W ') Yaml.dump (data,f) f.close ()
Python parsing yaml files