[ PROMPT_NODE_27126 ]
io_formats
[ SKILL_DOCUMENTATION ]
# Pymatgen 输入/输出与文件格式参考
本参考文档记录了 Pymatgen 强大的输入/输出功能,支持读取和写入 100 多种结构和计算数据文件格式。
## 通用 I/O 理念
Pymatgen 通过 `from_file()` 和 `to()` 方法为文件操作提供统一接口,并根据文件扩展名自动检测格式。
### 读取文件
python
from pymatgen.core import Structure, Molecule
# 自动格式检测
struct = Structure.from_file("POSCAR")
struct = Structure.from_file("structure.cif")
mol = Molecule.from_file("molecule.xyz")
# 显式指定格式
struct = Structure.from_file("file.txt", fmt="cif")
### 写入文件
python
# 写入文件(格式由扩展名推断)
struct.to(filename="output.cif")
struct.to(filename="POSCAR")
struct.to(filename="structure.xyz")
# 获取字符串表示而不写入文件
cif_string = struct.to(fmt="cif")
poscar_string = struct.to(fmt="poscar")
## 结构文件格式
### CIF (晶体学信息文件)
晶体学数据的标准格式。
python
from pymatgen.io.cif import CifParser, CifWriter
# 读取
parser = CifParser("structure.cif")
structure = parser.get_structures()[0] # 返回结构列表
# 写入
writer = CifWriter(struct)
writer.write_file("output.cif")
# 或使用便捷方法
struct = Structure.from_file("structure.cif")
struct.to(filename="output.cif")
**关键特性:**
- 支持对称性信息
- 可包含多个结构
- 保留空间群和对称操作
- 处理部分占位情况
### POSCAR/CONTCAR (VASP)
VASP 的结构格式。
python
from pymatgen.io.vasp import Poscar
# 读取
poscar = Poscar.from_file("POSCAR")
structure = poscar.structure
# 写入
poscar = Poscar(struct)
poscar.write_file("POSCAR")
# 或使用便捷方法
struct = Structure.from_file("POSCAR")
struct.to(filename="POSCAR")
**关键特性:**
- 支持选择性动力学
- 可包含速度信息 (XDATCAR 格式)
- 保留晶格和坐标精度
### XYZ
简单的分子坐标格式。
python
# 用于分子
mol = Molecule.from_file("molecule.xyz")
mol.to(filename="output.xyz")
# 用于结构(笛卡尔坐标)
struct.to(filename="structure.xyz")
### PDB (蛋白质数据库)
生物分子的通用格式。
python
mol = Molecule.from_file("protein.pdb")
mol.to(filename="output.pdb")
#