Reading YAML in Python
- How yaml.safe_load() turns your YAML into a Python dictionary
- How to look up specific values: config['clock_period_ns']
- Recognising — not writing — the 3 lines of Python every script has
Your YAML file is a static text file. To actually use its values — to run a simulation, generate a report, or pass settings to a tool — something has to read it. In semiconductor workflows, that “something” is almost always a Python script.
Why Python?
Python is the most widely used scripting language in semiconductor design. Engineers write thin Python wrappers that:
- Read a YAML config file — the engineer-friendly description of what to run
- Pass the values into the EDA tool — synthesis, simulation, place-and-route
- Collect results back — often writing more YAML files for the next stage
You do not have to write these Python scripts yourself — but understanding what they do with your YAML helps you write better YAML.
The YAML file becomes a Python dictionary
This is the single most important idea in this lesson:
When Python reads a YAML file, the file turns into a dictionary — a table of key-value pairs that Python can look up instantly.
Every key in your YAML becomes a key in the dictionary. Every value keeps its type: numbers stay numbers, text stays text, lists stay lists.
How Python Reads Your YAML File
Step 0 / 4Start — your YAML file
Installing the YAML library
Python does not read YAML on its own. You need one small library called PyYAML. It is installed with a single command:
pip install pyyaml pip is Python’s package installer. You usually run this once on your machine and never think about it again. On shared company servers, it may already be installed — ask your tools team.
The three lines that read YAML
Here is every Python script that reads YAML, stripped down to its essence:
import yaml
with open('chip_config.yaml') as f:
config = yaml.safe_load(f) Line by line:
import yaml— tells Python “I want to use the YAML library”open('chip_config.yaml')— opens the file for readingyaml.safe_load(f)— reads the file and returns a Python dictionary calledconfig
That’s it. Three lines, and your YAML is loaded. Always use safe_load — not load. safe_load refuses to execute arbitrary code hidden in YAML files, which is safer for files coming from email, version control, or the web.
Accessing values
Once config is loaded, you reach into it with square brackets, using the key name:
print(config['chip_name']) # TitanCore
print(config['frequency_mhz']) # 100
print(config['debug_mode']) # True For nested YAML, chain the brackets:
# YAML has base_config with technology nested inside it
print(config['base_config']['technology']) # 28nm For lists, use a number (starting from 0 — the first item is [0], not [1]):
# YAML: team: [Alice, Bob, Carol]
print(config['team'][0]) # Alice
print(config['team'][2]) # Carol A complete, runnable example
Here is the YAML file:
And here is the complete Python script that reads it and uses every value:
import yaml
with open('chip.yaml') as f:
config = yaml.safe_load(f)
chip = config['chip_name']
freq = config['frequency_mhz']
volt = config['voltage']
debug = config['debug_mode']
print("Building", chip, "at", freq, "MHz,", volt, "V")
if debug:
print("Debug mode is ON — extra logs will be collected")
else:
print("Debug mode is OFF — production run") Run it and you get:
Building TitanCore at 100 MHz, 1.2 V
Debug mode is ON — extra logs will be collected Notice what happened: the YAML file is the human-editable interface. The Python script is the machinery. You change the .yaml file (no coding), and the script behaves differently. This separation is the whole point of using YAML.
Key takeaways
- YAML files become Python dictionaries after
yaml.safe_load() - Install the library once:
pip install pyyaml - Three lines of Python read any YAML file —
import yaml,open(...),yaml.safe_load() - Access values with
config['key'], nested withconfig['a']['b'], lists withconfig['list'][0] - Always use
safe_load(notload) — it’s safer with files from outside sources
Next we will see the reverse direction: how a Python script can produce YAML files of its own — useful when a tool needs to write reports or hand results to the next stage.