Skip to main content

add dump_to_bytes method that returns UTF-8 encoded bytes array

Project description


This plug-in adds a method dump_to_bytes to the ruamel.yaml.YAML instance that returns a (UTF-8) bytes array


The module can be installed from PyPI using:

pip install ruamel.yaml.bytes

This module is dependent on ruamel.yaml, so you do not have to explicitly make your module depending on both.


import ruamel.yaml

yaml = ruamel.yaml.YAML(typ=['rt', 'bytes'])
data  = dict(abc=42, help=['on', 'its', 'way'])
print('retval', yaml.dump_to_bytes(data).decode('utf-8'))
print('>>>> done')

which gives:

retval abc: 42
- on
- its
- way
>>>> done

Please note that there is no final newline added to the bytes array returned, and that the >>>> done is on the next line is caused by the print() function adding a newline by default. Alternatively the first call to print could be:

print('retval', yaml.dump_to_bytes(data, add_final_eol=True).decode('utf-8'), end='')

with the same effect.

.dump_to_bytes() can be shortened to .dumpb()


  • initial plug-in version

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ruamel.yaml.bytes-0.1.0.tar.gz (13.0 kB view hashes)

Uploaded source

Built Distribution

ruamel.yaml.bytes-0.1.0-py2.py3-none-any.whl (4.0 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page