check.py
import os
from pathlib import Path
import sys
import json
from ruamel.yaml import YAML
yaml = YAML(pure=True)
yaml.sort_base_mapping_type_on_output = None
yaml.indent(mapping=2, sequence=4, offset=2)
def yamlCheck(file: os.PathLike):
with open(file, 'r', encoding='utf8') as yr:
data = yaml.load(yr)
print(data)
check = json.dumps(data, indent=4, ensure_ascii=False)
print(check)
if __name__ == "__main__":
yamlCheck('test.yaml')
test1.yaml, with error.
a:
- b: 2
- 123
- 2000-12-26
test2.yaml, runs ok
a:
- b: 2
- 123
- 111-12-26
test2.yaml, runs ok
a:
- b: 2
- 123
- 2000-12-26111
runs ok means the code runs without error, json.dumps load the OrderedDict data well.
with the output of print(data) I think ruamel.yaml has parse that xxxx-xx-xx formatted data as datetime and construct a datetime object with it, which causes the json.dumps function's error.
So what is the standard of the ruamel.yaml to decide to parse the value as datetime?