Writing YAML from Python

6 min read · Beginner
You will learn
  • How a script writes a YAML report file after a run
  • How yaml.dump() converts Python values back to YAML text
  • Keeping written reports clean and readable with sort_keys / default_flow_style

The reverse direction is just as important: sometimes a Python script needs to create a YAML file. Common situations in semiconductor design:

  • After a simulation run, save the resulting power/timing numbers as YAML for the next stage
  • After synthesis, write a report file summarising the area and critical path
  • When generating many test cases, produce one small YAML per test

The good news: writing YAML is just as simple as reading it.


From dictionary to file: yaml.dump()

yaml.dump() is the opposite of yaml.safe_load(). You hand it a Python dictionary and a file, and it writes out a clean YAML file.

First, install the library (one-time, same as for reading):

pip install pyyaml

A complete write example:

import yaml

config = dict(
    project_name='My Chip',
    frequency_mhz=100,
    debug_mode=True,
)

with open('output.yaml', 'w') as f:
    yaml.dump(config, f, default_flow_style=False)

Run it, and the script creates output.yaml in the current folder containing exactly the values you put into the dictionary.


The three lines that write YAML

Strip away everything except the essentials:

import yaml

with open('output.yaml', 'w') as f:
    yaml.dump(my_data, f, default_flow_style=False)

What each piece does:

  • open('output.yaml', 'w') — opens the file for writing (the 'w' is important; without it Python refuses to overwrite)
  • yaml.dump(my_data, f, ...) — writes the Python data into that file as YAML
  • default_flow_style=False — tells PyYAML to use the readable multi-line format, not a cramped one-line “flow” style

The flow style trap

If you forget default_flow_style=False, PyYAML tries to help by putting everything on one line — something like [project_name: My Chip, frequency_mhz: 100, debug_mode: true] all crammed together inside braces.

That is technically valid YAML, but nobody wants to read it. With default_flow_style=False, you get the clean, proper format instead:

· = one space

Rule of thumb: always pass default_flow_style=False unless you explicitly want one-liner output.


Going the other way: reading what you wrote

Whatever yaml.dump() writes, yaml.safe_load() can read back. This is how EDA tool chains pass data between stages without writing custom binary formats — each stage speaks plain YAML.

import yaml

# Stage 1: write
results = dict(area_um2=12450, max_freq_mhz=1200)
with open('stage1.yaml', 'w') as f:
    yaml.dump(results, f, default_flow_style=False)

# Stage 2: read
with open('stage1.yaml') as f:
    prev = yaml.safe_load(f)
print(prev['area_um2'])   # 12450

Your YAML file acts as the contract between the two scripts. If someone later wants to inspect or tweak what stage 1 handed off, they can open the YAML file in any text editor — no special tool required.


Key takeaways

  • yaml.dump(data, file) turns a Python dictionary into a YAML file
  • Open the file with 'w' to allow writing: open('out.yaml', 'w')
  • Always pass default_flow_style=False for readable multi-line output
  • Scripts in a pipeline can pass data via YAML files — one writes, the next reads — no special format needed

Next, we will see what happens when things go wrong: syntax mistakes in YAML, missing files, and how Python reports them.