Skip to content

Instantly share code, notes, and snippets.

@duke8585
Created November 1, 2022 13:35
Show Gist options
  • Save duke8585/491e6ab8b427164b767269791f3e0a14 to your computer and use it in GitHub Desktop.
Save duke8585/491e6ab8b427164b767269791f3e0a14 to your computer and use it in GitHub Desktop.
import yaml
with open("test.yml", "r") as stream:
try:
y_structure = yaml.safe_load(stream)
# will be dict of dict / list etc.
print(type(y_structure))
print(yaml.dump(y_structure, default_flow_style=False))
except yaml.YAMLError as exc:
print(exc)
[Running] python -u "/Users/max.rieger/Documents/sandbox/pyyml/parse_yml.py"
<class 'dict'>
1_scalar: 1
2_dict:
c: 2
d: 3
3_list:
- 1
- 3
- 5
- of_dict: 7
- of_list:
- 9
- 11
- 13
- 15
- 17
- 19
4_list_v2:
- 2
- 4
- 6
- 8
- 10
5_ts:
canonical: 2001-12-15 02:59:43.100000+00:00
date (00:00:00Z): 2002-12-14
no time zone (Z): 2001-12-15 02:59:43.100000
space separated: 2001-12-14 21:59:43.100000-05:00
valid iso8601: 2001-12-14 21:59:43.100000-05:00
6_fixed_type1: 123.456
7_fixed_type2: 123
8_l: true
zz_ref: https://betterprogramming.pub/everything-you-need-to-know-about-yaml-fdbb7acf6db6
1_scalar: 1
2_dict:
c: 2
d: 3
3_list:
- 1
- 3
- 5
- of_dict: 7
- of_list: [9, 11, 13, 15, 17, 19]
4_list_v2: [2, 4, 6, 8, 10]
5_ts:
canonical: 2001-12-15T02:59:43.1Z
valid iso8601: 2001-12-14t21:59:43.10-05:00
space separated: 2001-12-14 21:59:43.10 -5
no time zone (Z): 2001-12-15 2:59:43.10
date (00:00:00Z): 2002-12-14
6_fixed_type1: !!float 123.456
7_fixed_type2: !!int 123
8_l: True
zz_ref: https://betterprogramming.pub/everything-you-need-to-know-about-yaml-fdbb7acf6db6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment