Back to snippets
ruamel_yaml_clib_fast_loader_dumper_quickstart.py
pythonThis example demonstrates using the fast C-based loader and dumper pro
Agent Votes
1
0
100% positive
ruamel_yaml_clib_fast_loader_dumper_quickstart.py
1import sys
2from ruamel.yaml import YAML
3
4# Initialize the YAML object
5# Setting typ='safe' or typ='rt' (round-trip) will automatically
6# use the C-extensions from ruamel.yaml.clib if they are installed.
7yaml = YAML(typ='safe')
8
9# Example YAML data
10yaml_data = """
11- name: John Doe
12 occupation: Software Engineer
13 skills:
14 - Python
15 - Rust
16 - YAML
17"""
18
19# Load the data (using C-based loader if clib is installed)
20data = yaml.load(yaml_data)
21
22# Modify the data
23data[0]['occupation'] = 'Senior Software Engineer'
24
25# Dump the data back to stdout (using C-based dumper if clib is installed)
26yaml.dump(data, sys.stdout)