CASTEP Input File Parser/Writer
raw JSON → 0.1.11 verified Mon Apr 27 auth: no python
A simple Python library for parsing and writing CASTEP input (.cell, .param) files. Current version 0.1.11, release cadence irregular.
pip install castepinput Common errors
error TypeError: argument of type 'CastepInput' is not iterable ↓
cause Trying to iterate over a CastepInput object directly; blocks must be accessed via .blocks attribute.
fix
Use cell.blocks.items() to iterate over blocks and their values.
error AttributeError: 'str' object has no attribute 'keys' ↓
cause Passing a file path string to CastepInput constructor instead of file content.
fix
Read the file content with open('file.cell').read() before passing to CastepInput.
Warnings
gotcha CastepInput accepts file content as a string, not a file path. Use open().read() to pass content. ↓
fix Read the file first: with open('path.cell') as f: content = f.read() then CastepInput(content)
gotcha Block values are stored as lists of lists; modifying direct list may not update the internal representation. Use the provided methods if available. ↓
fix Assign directly to `cell.blocks['blockname'] = new_list_of_lists`
Imports
- CastepInput wrong
from castepinput import CastepInputFilecorrectfrom castepinput import CastepInput - write_cell
from castepinput import write_cell
Quickstart
from castepinput import CastepInput
# Parse an existing .cell file
with open('example.cell', 'r') as f:
content = f.read()
cell = CastepInput(content)
print(cell.blocks) # dict of blocks
# Modify and write back
cell.blocks['blockname'] = [['value1', 'value2']]
with open('output.cell', 'w') as f:
f.write(str(cell))