[关闭]
@nalan90 2018-06-20T10:20:23.000000Z 字数 1306 阅读 3000

专题十一 pyyaml库常用操作

Python高效编程技巧实战


yaml文件操作
  1. In [23]: cat init-server.yml
  2. roles:
  3. jdy-web:
  4. public_ports:
  5. - '20000/tcp'
  6. - '20003/tcp'
  7. web:
  8. public_ports:
  9. - '80/tcp'
  10. - '443/tcp'
  11. squid:
  12. public_ports:
  13. - '3128/tcp'
  14. dns:
  15. public_ports:
  16. - '53/udp'
  17. - '53/tcp'
  18. log:
  19. public_ports:
  20. - '601/tcp'
  21. - '514/udp'
  22. - '5044/tcp'
  23. kv_config:
  24. vintage_ss_bypass_ips: '47.91.129.91 47.52.7.116 172.168.100.0/24 139.162.107.188 47.89.58.99/22 47.90.40.63/22'
  25. ci:
  26. jenkins_http_port: 11881
  27. gitlab_url: 'https://git.sunfund.com'
  28. gitlab_runner_token: 'Y2ixs1d1Zdq7o_4gZdtk'
  29. gitlab:
  30. gitlab_http_port: 11180
  31. rsync:
  32. ssh_port: 11222
  33. server: 'bak-01.ns.9dy.in'
  34. ----------
  35. In [15]: import yaml
  36. ## yaml将yml格式的文件处理成一个dict object
  37. In [16]: with open('init-server.yml','r') as f:
  38. ....: data = yaml.load(f)
  39. In [17]: print type(data)
  40. <type 'dict'>
  41. In [24]: data
  42. Out[24]:
  43. {'kv_config': {'ci': {'gitlab_runner_token': 'Y2ixs1d1Zdq7o_4gZdtk',
  44. 'gitlab_url': 'https://git.sunfund.com',
  45. 'jenkins_http_port': 11881},
  46. 'gitlab': {'gitlab_http_port': 11180},
  47. 'rsync': {'server': 'bak-01.ns.9dy.in', 'ssh_port': 11222},
  48. 'vintage_ss_bypass_ips': '47.91.129.91 47.52.7.116 172.168.100.0/24 139.162.107.188 47.89.58.99/22 47.90.40.63/22'},
  49. 'roles': {'dns': {'public_ports': ['53/udp', '53/tcp']},
  50. 'jdy-web': {'public_ports': ['20000/tcp', '20003/tcp']},
  51. 'log': {'public_ports': ['601/tcp', '514/udp', '5044/tcp']},
  52. 'squid': {'public_ports': ['3128/tcp']},
  53. 'web': {'public_ports': ['80/tcp', '443/tcp']}}}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注