Last active
December 18, 2024 09:38
-
-
Save astrojuanlu/af35bbbe949b9d9c1ea95224e9778f49 to your computer and use it in GitHub Desktop.
Experiments with YAML and JSON5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document: | |
key1: "value1" | |
key2: "value2" | |
list1: | |
- "item1" | |
- "item2" | |
- dictionary1: | |
key3: "value3" | |
key4: "value4" | |
list2: | |
- "item3" | |
- "item4" | |
list3: | |
- "item5" | |
- "item6" | |
key5: "value5" | |
document2: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document: { | |
key1: "value1", | |
key2: "value2", | |
list1: [ | |
"item1", | |
"item2", | |
{ | |
dictionary1: { | |
key3: "value3", | |
key4: "value4", | |
list2: [ | |
"item3", | |
"item4" | |
], | |
list3: [ | |
"item5" | |
] | |
} | |
}, | |
"item6" | |
], | |
key5: "value5" | |
} | |
document2: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
document: { | |
key1: "value1", | |
key2: "value2", | |
list1: [ | |
"item1", | |
"item2", | |
{ | |
dictionary1: { | |
key3: "value3", | |
key4: "value4", | |
list2: [ | |
"item3", | |
"item4" | |
], | |
list3: [ | |
"item5" | |
] | |
} | |
}, | |
"item6" | |
], | |
key5: "value5" | |
}, | |
document2: 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json5 | |
from ruamel.yaml import YAML | |
yaml = YAML() | |
with open("example.yaml") as fh: | |
content = yaml.load(fh.read()) | |
with open("example_clean.yaml") as fh: | |
content_clean = yaml.load(fh.read()) | |
with open("example_json5.yaml") as fh: | |
content_json5_w_yaml = yaml.load(fh.read()) | |
with open("example_json5.yaml") as fh: | |
content_json5 = json5.load(fh) | |
assert content == content_clean == content_json5_w_yaml == content_json5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment