Skip to content

Instantly share code, notes, and snippets.

import yaml
names_yaml = """
- 'eric'
- 'justin'
- 'mary-kate'
"""
with open('names.yaml', 'w') as file:
yaml.dump(names, file)
@PythonDotLand
PythonDotLand / parse_yaml_string.py
Created April 19, 2021 18:09
How to parse a YAML string with Python
>>> import yaml
>>>
>>> names_yaml = """
... - 'eric'
... - 'justin'
... - 'mary-kate'
... """
>>>
>>> names = yaml.safe_load(names_yaml)
>>> names
@PythonDotLand
PythonDotLand / open_yaml.py
Created April 19, 2021 18:08
How to read YAML with Python
>>> import yaml
>>> with open('config.yml', 'r') as file
... prime_service = yaml.safe_load(file)
>>> prime_service
{'rest':
{ 'url': 'https://example.org/primenumbers/v1',
'port': 8443
},
'prime_numbers': [2, 3, 5, 7, 11, 13, 17, 19]}
@PythonDotLand
PythonDotLand / example.yaml
Created April 19, 2021 18:07
Example of a YAML config file
rest:
url: "https://example.org/primenumbers/v1"
port: 8443
prime_numbers: [2, 3, 5, 7, 11, 13, 17, 19]