TypeError: load() missing 1 required positional argument: ‘Loader’ in Google Colab

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 … Read more

How can I control what scalar form PyYAML uses for my data?

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 … Read more

PyYAML dump format

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 … Read more

Save/dump a YAML file with comments in PyYAML

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() … Read more

Reading YAML file with Python results in yaml.composer.ComposerError: expected a single document in the stream

The yaml documents are separated by —, and if any stream (e.g. a file) contains more than one document then you should use the yaml.load_all function rather than yaml.load. The code: import yaml stream = open(“test”, “r”) docs = yaml.load_all(stream, yaml.FullLoader) for doc in docs: for k,v in doc.items(): print k, “->”, v print “\n”, … Read more

Can PyYAML dump dict items in non-alphabetical order?

If you upgrade PyYAML to 5.1 version, now, it supports dump without sorting the keys like this: yaml.dump(data, sort_keys=False) As shown in help(yaml.Dumper), sort_keys defaults to True: Dumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True) (These are passed as kwargs to yaml.dump)

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)