f90nml

raw JSON →
1.5 verified Fri May 01 auth: no python

A Python module for reading and writing Fortran 90 namelist files. Current version is 1.5 with irregular release cadence. Supports parsing, modifying, and writing namelist files, preserving structure and comments.

pip install f90nml
error AttributeError: module 'f90nml' has no attribute 'Namelist'
cause Typo in class name: correct is 'Namelist' with capital 'N'.
fix
Use: from f90nml import Namelist or f90nml.Namelist
error TypeError: write() argument must be str, not _io.TextIOWrapper
cause Passing a file object to write() without specifiying mode 'w' or using deprecated string path.
fix
Use: with open('file.nml', 'w') as f: nml.write(f)
error ValueError: Invalid key: 'mykey' - key must be a string
cause Keys passed to Namelist constructor or assignments must be strings.
fix
Ensure all namelist group and variable names are strings.
error AttributeError: 'Cogroup' object has no attribute 'append'
cause Cogroup does not support append; use list-like indexing or extend.
fix
Use: nml['group']['key'].extend([value]) or nml['group']['key'] = f90nml.Cogroup([value1, value2])
deprecated The write() method with a file path is deprecated in favor of using open() and write() with a file object.
fix Use: with open('file.nml', 'w') as f: nml.write(f)
breaking Duplicate keys behavior changed: in version 1.4, duplicate keys are now stored as Cogroup objects, not lists. Code expecting plain lists will break.
fix Handle duplicate keys via f90nml.Cogroup or use nml.todict() with dups=True to get list representation.
gotcha Empty lists in namelist are formatted as comma-delimited blanks (e.g., ',,') instead of an empty line. This can cause confusion when re-parsing.
fix Use custom formatting or work with string representation if needed.
gotcha The parser treats unquoted strings case-sensitively. Fortran normally treats them case-insensitively, leading to unexpected mismatches.
fix Manually convert string values to lowercase/uppercase as needed.

Basic usage: read, modify, write namelist files.

import f90nml
import os

# Read a namelist file
nml = f90nml.read('path/to/namelist')

# Access a variable
print(nml['namelist_name']['variable_name'])

# Write back
nml.write('output.nml')

# Create from dict
data = {'namelist': {'var1': 1.0, 'var2': 2}}
nml = f90nml.Namelist(data)
nml.write('new.nml')