Dump in PyYaml as utf-8
Found the answer myself. I just had to dump it with the argument allow_unicode=True Source: http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/
Found the answer myself. I just had to dump it with the argument allow_unicode=True Source: http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/
Important security disclaimer: I would strongly discourage anyone downgrading PyYAML to 5.3.1, as versions before 5.4 all have a known CVE — CVE-2020-14343. This issue was caused by an incompatibility of PyYAML<6.0.1 compiled under the version of Cython>=3.0, released on July 17, 2023. This has now been fixed by a new release of PyYAML, tagged …
For None type to be read from python use null in yaml A YAML file test.yml like this foo: null bar: null will be read by python as import yaml test = yaml.load(open(‘./test.yml’)) print(test) foo: None bar: None
The problem lies in the fact that the YAML Resolver is set up to match floats as follows: Resolver.add_implicit_resolver( u’tag:yaml.org,2002:float’, re.compile(u”’^(?:[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+][0-9]+)? |\\.[0-9_]+(?:[eE][-+][0-9]+)? |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]* |[-+]?\\.(?:inf|Inf|INF) |\\.(?:nan|NaN|NAN))$”’, re.X), list(u’-+0123456789.’)) whereas the YAML spec specifies the regex for scientific notation as: -? [1-9] ( \. [0-9]* [1-9] )? ( e [-+] [1-9] [0-9]* )? the latter makes the …
Now, the load() function requires parameter loader=Loader. If your YAML file contains just simple YAML (str, int, lists), try to use yaml.safe_load() instead of yaml.load(). And If you need FullLoader, you can use yaml.full_load(). Starting from pyyaml>=5.4, it doesn’t have any discovered critical vulnerabilities, pyyaml status. source: https://stackoverflow.com/a/1774043/13755823 yaml.safe_load() should always be preferred unless you …
Falling in love with @lbt’s approach, I got this code: import yaml def str_presenter(dumper, data): if len(data.splitlines()) > 1: # check for multiline string return dumper.represent_scalar(‘tag:yaml.org,2002:str’, data, style=”|”) return dumper.represent_scalar(‘tag:yaml.org,2002:str’, data) yaml.add_representer(str, str_presenter) # to use with safe_dump: yaml.representer.SafeRepresenter.add_representer(str, str_presenter) It makes every multiline string be a block literal. I was trying to avoid the …
Below, ruamel.yaml is used instead. ruamel.yaml is actively maintained. Unlike PyYAML, ruamel.yaml supports: YAML <= 1.2. PyYAML only supports YAML <= 1.1. This is vital, as YAML 1.2 intentionally breaks backward compatibility with YAML 1.1 in several edge cases. This would usually be a bad thing. In this case, this renders YAML 1.2 a strict …
If you are using block structured YAML, you can use the python package¹ ruamel.yaml which is a derivative of PyYAML and supports round trip preservation of comments: import sys import ruamel.yaml yaml_str = “””\ # example name: # details family: Smith # very common given: Alice # one of the siblings “”” yaml = ruamel.yaml.YAML() …
I don’t know a lot about YAML but I’ll give it a shot: Tags are used to denote types. A tag is declared using ! as you have seen from the “refcard” there. The %TAG directive is kind of like declaring a shortcut to a tag. I’ll demonstrate with PyYaml. PyYaml can parse the secondary …
You can use safe_dump instead of dump. Just keep in mind that it won’t be able to represent arbitrary Python objects then. Also, when you load the YAML, you will get a str object instead of unicode.