Advertisement
easternnl

Python with Yaml example

Apr 4th, 2019
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import sys
  2. from ruamel.yaml import YAML
  3.  
  4. yaml = YAML()
  5.  
  6. # read from disk:
  7. #with open("test2.yml") as stream:
  8. #    code = yaml.load(stream)
  9.  
  10. # read from code:
  11. code = yaml.load("""
  12. computers:
  13.  - name: Lenovo P50
  14.    cpu: i7 6820HQ
  15.    disks:
  16.      - Samsung 256 SATA
  17.      - Samsung 970 EVO Plus NVME
  18.    memory: 32GB
  19.  - name: HP Z800
  20.    cpu: Intel Xeon L5440
  21.    memory: 48GB
  22.    disks:
  23.      - Crucial 256 GB SSD
  24.      - Crucial 1TB SSD
  25.      - WD 500 GB Enterprise
  26.      - WD 500 GB Enterprise
  27.      - WD 500 GB Enterprise
  28.      - WD 500 GB Enterprise
  29. """)
  30.  
  31. print(code)
  32.  
  33. for x in code['computers']:
  34.     print(x)
  35.  
  36.     print(x['name'])
  37.  
  38.  
  39. print (code['computers'][0]['cpu'])
  40. print (code['computers'])
  41. #yaml.dump(code, sys.stdout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement