I would like to know how to easily read and write values from a Fortran namelist file in Python.
There is a module called f90nml which reads/writes Fortran namelists. With this module you can read a namelist into a nested Python dictionary:
import f90nml
nml = f90nml.read('sample.nml')
The values can be edited and written back to disk.
nml['config_nml']['steps'] = 432
nml.write('new_sample.nml')
The package can be installed with pip
:
pip install f90nml
Source code is at https://github.com/marshallward/f90nml
I wrote a python module to read/write Fortran namelist files because I couldn't find anything that quite worked for me: https://github.com/leifdenby/namelist_python
It:
I've written quite a few tests too, if there are any namelist files that don't parse correctly let me know and I'll have a look.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With