yamlloader is a Python library (version 1.6.0) that provides ordered YAML loaders and dumpers for PyYAML, ensuring that the order of items in dictionaries is preserved when loading from or dumping to YAML files. It offers faster C-version implementations and automatically falls back to pure Python versions if C bindings are unavailable. The library is actively maintained, with recent updates for Python 3.14 support.
pip install yamlloaderVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load and dump YAML content using `yamlloader`'s `CLoader` and `CDumper` to ensure dictionary order is preserved. It highlights how `yaml.load` is used with the custom loader and how the order is maintained in the resulting Python object, even for standard dictionaries in Python 3.7+.
Upgrade Python to 3.8+ or pin `yamlloader` to `<1.3.0` for Python 3.7 environments.
Always explicitly specify a loader when calling `yaml.load()`. For safe loading of untrusted input, use `yaml.safe_load()` or `yaml.load(file, Loader=yaml.SafeLoader)`. When using `yamlloader`, ensure you pass its specific loaders: `yaml.load(file, Loader=yamlloader.ordereddict.CLoader)` or `yaml.load(file, Loader=yamlloader.ordereddict.CSafeLoader)`.
For consistent ordered dictionary behavior, ensure you are using `yamlloader` version `1.0.0` or newer. For Python 3.7+, standard `dict`s preserve insertion order, but explicit `OrderedDict`s or `yamlloader`'s wrappers provide a stronger guarantee and consistent API.
To ensure C-version performance, install `libyaml-dev` (or equivalent for your OS) and `Cython` before installing PyYAML or `yamlloader`. Check `yaml.cyaml` exists in your environment to verify C-binding availability.
Install PyYAML: `pip install pyyaml` (or simply `pip install yamlloader` which should pull in PyYAML as a dependency).
Always provide a `Loader` argument. For `yamlloader`'s ordered loading, use `yaml.load(file_obj, Loader=yamlloader.ordereddict.CLoader)`. For general safe loading, use `yaml.safe_load(file_obj)` or `yaml.load(file_obj, Loader=yaml.SafeLoader)`.
For extremely large YAML files, consider processing them in chunks if the structure allows, or using alternative streaming parsers not offered by `yamlloader`/`PyYAML`. Ensure your system has sufficient RAM for the file size.
Replace all tab characters with spaces for indentation in your YAML file. Many text editors have 'convert tabs to spaces' functionality. Ensure consistent indentation levels (e.g., always 2 or 4 spaces).