Skip to content

Instantly share code, notes, and snippets.

@easternnl
Created March 6, 2020 09:48
Show Gist options
  • Save easternnl/8297e8aa16198c155a533543456d6f40 to your computer and use it in GitHub Desktop.
Save easternnl/8297e8aa16198c155a533543456d6f40 to your computer and use it in GitHub Desktop.
YAML with Python Example
import sys
from ruamel.yaml import YAML
yaml = YAML()
# read from disk:
#with open("test2.yml") as stream:
# code = yaml.load(stream)
# read from code:
code = yaml.load("""
computers:
- name: Lenovo P50
cpu: i7 6820HQ
disks:
- Samsung 256 SATA
- Samsung 970 EVO Plus NVME
memory: 32GB
- name: HP Z800
cpu: Intel Xeon L5440
memory: 48GB
disks:
- Crucial 256 GB SSD
- Crucial 1TB SSD
- WD 500 GB Enterprise
- WD 500 GB Enterprise
- WD 500 GB Enterprise
- WD 500 GB Enterprise
""")
print(code)
for x in code['computers']:
print(x)
print(x['name'])
print (code['computers'][0]['cpu'])
print (code['computers'])
#yaml.dump(code, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment