其中cif文件内容如下 data_global _chemical_name_mineral 'Agardite-' loop_ _publ_author_name 'Hess H' _journal_name_full 'Neues Jahrbuch fur Mineralogie, Monatshefte' _journal_volume 1983 _journal_year 1983 _journal_page_first 385 _journal_page_last 392 _publ_section_title ; Die kristallstruktur des chlorotils, ; _database_code_amcsd 0014774 _chemical_compound_source 'Clara mine, Black Forest, Baden-Wurttemberg, Germany' _chemical_formula_sum 'Ce.707 Cu6 As3 O21 H12' _cell_length_a 13.605 _cell_length_b 13.605 _cell_length_c 5.917 _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 120 _cell_volume 948.482 _exptl_crystal_density_diffrn 3.688 _symmetry_space_group_name_H-M 'P 63/m' loop_ _space_group_symop_operation_xyz 'x,y,z' '-x+y,-x,1/2-z' 'x-y,x,1/2+z' 'y,-x+y,-z' '-y,x-y,z' 'x,y,1/2-z' '-x,-y,1/2+z' 'x-y,x,-z' '-x+y,-x,z' '-y,x-y,1/2-z' 'y,-x+y,1/2+z' '-x,-y,-z' loop_ _atom_site_label _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z Ce 0.66667 0.33333 0.25000 Cu
时间: 2025-06-24 17:37:26 浏览: 19
### 解析 CIF 文件并提取所需信息
以下是一个完整的解决方案,基于 Python 编程语言,使用 `ase` 和 `pymatgen` 库来解析 CIF 文件,并提取原子坐标、空间点群信息以及伪元素定义。最终将结果保存为 TXT 文件。
---
#### 1. 安装依赖库
确保已安装所需的 Python 库:
```bash
pip install ase pymatgen
```
---
#### 2. 提取 CIF 文件中的信息
以下是实现该功能的核心代码:
```python
import os
from ase.io import read
from pymatgen.core.structure import Structure
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
def extract_cif_data(cif_filepath):
"""
从 CIF 文件中提取原子坐标、空间点群和其他相关信息。
"""
try:
# 使用 ASE 读取 CIF 文件
atoms = read(cif_filepath)
# 获取晶胞参数和原子坐标
cell_parameters = atoms.cell[:]
atomic_positions = [(atom.symbol, *atom.position) for atom in atoms]
# 使用 Pymatgen 分析空间点群
structure = Structure.from_file(cif_filepath)
space_group_analyzer = SpacegroupAnalyzer(structure)
space_group_symbol = space_group_analyzer.get_space_group_symbol()
# 返回提取的数据
return {
"filename": cif_filepath,
"cell_parameters": cell_parameters.tolist(),
"atomic_positions": atomic_positions,
"space_group": space_group_symbol
}
except Exception as e:
print(f"Error processing {cif_filepath}: {str(e)}")
return None
def save_to_txt(data_dict, output_dir="output"):
"""
将提取的数据保存为 TXT 文件。
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
base_filename = os.path.splitext(os.path.basename(data_dict["filename"]))[0] + ".txt"
output_path = os.path.join(output_dir, base_filename)
with open(output_path, "w") as f:
f.write(f"Filename: {data_dict['filename']}\n")
f.write(f"Cell Parameters:\n{data_dict['cell_parameters']}\n\n")
f.write(f"Space Group Symbol: {data_dict['space_group']}\n\n")
f.write("Atomic Positions:\nSymbol X Y Z\n")
for symbol, x, y, z in data_dict["atomic_positions"]:
f.write(f"{symbol} {x:.6f} {y:.6f} {z:.6f}\n")
print(f"Data saved to {output_path}")
def process_directory(directory_path):
"""
批量处理目录下的所有 CIF 文件。
"""
cif_files = [
os.path.join(directory_path, fname)
for fname in os.listdir(directory_path)
if fname.lower().endswith(".cif")
]
for cif_file in cif_files:
extracted_data = extract_cif_data(cif_file)
if extracted_data:
save_to_txt(extracted_data)
# 主程序入口
input_directory = "./cifs/" # 输入的 CIF 文件所在目录
process_directory(input_directory)
```
---
#### 3. 功能详解
##### (1)ASE 部分[^1]
- **`read()` 函数**:用于加载 CIF 文件,返回一个包含晶体结构的对象。
- **`atoms.cell[:]`**:获取晶胞矩阵(单位为Å)。
- **`[(atom.symbol, *atom.position)]`**:提取每个原子的符号及其三维坐标。
##### (2)Pymatgen 部分[^2]
- **`Structure.from_file()`**:从文件创建一个 `Structure` 对象。
- **`SpacegroupAnalyzer`**:分析晶体的空间对称性。
- **`get_space_group_symbol()`**:返回国际符号形式的空间点群。
##### (3)伪元素处理
如果 CIF 文件中包含伪元素(如 OH 或 H2O),它们通常会被视为独立的化学单元。ASE 和 Pymatgen 默认会保留这些单元的原始定义。因此无需特殊编码即可自动识别和记录这些伪元素的位置。
---
#### 4. 输出示例
假设输入文件名为 `example.cif`,则生成的 TXT 文件内容如下:
```
Filename: ./cifs/example.cif
Cell Parameters:
[[4.040, 0.000, 0.000], [0.000, 4.040, 0.000], [0.000, 0.000, 4.040]]
Space Group Symbol: Fd-3m
Atomic Positions:
Symbol X Y Z
Si 0.000000 0.000000 0.000000
Si 0.250000 0.250000 0.250000
OH 1.000000 1.000000 1.000000
H2O 2.000000 2.000000 2.000000
```
---
### 注意事项
1. 如果 CIF 文件中有错误或不兼容的内容,可能会导致解析失败。建议提前验证文件的有效性。
2. 对于非常规的伪元素定义,可能需要手动调整解析逻辑以满足具体需求。
---
阅读全文
相关推荐


















