提取CIF文件晶胞数据至CSV时遇字段不匹配错误求助
需求
批量提取文件夹中所有布局各异但标签统一的.cif文件内的晶胞长度、角度等数据,将每条数据写入CSV文件的一行。
更新说明
根据评论要求,补充说明输入CIF文件布局可变,并附上Traceback错误信息,感谢@CodeMonkey等的反馈。
示例CIF文件内容
####################################################################### # # Cambridge Crystallographic Data Centre # CCDC # ####################################################################### # # If this CIF has been generated from an entry in the Cambridge # Structural Database, then it will include bibliographic, chemical, # crystal, experimental, refinement or atomic coordinate data resulting # from the CCDC's data processing and validation procedures. # ####################################################################### data_PYRAZI _symmetry_cell_setting orthorhombic _symmetry_space_group_name_H-M 'P m n n' _symmetry_Int_Tables_number 58 _space_group_name_Hall '-P 2n 2' loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 x,y,z 2 1/2-x,1/2-y,1/2+z 3 x,-y,-z 4 1/2-x,1/2+y,1/2-z 5 -x,-y,-z 6 1/2+x,1/2+y,1/2-z 7 -x,y,z 8 1/2+x,1/2-y,1/2+z _cell_length_a 9.316(3) _cell_length_b 3.815(5) _cell_length_c 5.911(3) _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 90 _cell_volume 210.08 loop_ _atom_site_label _atom_site_type_symbol _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z N1 N 0.14850 0.00000 0.00000 C1 C 0.07280 0.11130 0.17330 H1 H 0.13000 0.19200 0.31700 C1B C 0.07280 -0.11130 -0.17330 C1F C -0.07280 0.11130 0.17330 C1D C -0.07280 -0.11130 -0.17330 H1B H 0.13000 -0.19200 -0.31700 N1D N -0.14850 0.00000 0.00000 H1F H -0.13000 0.19200 0.31700 H1D H -0.13000 -0.19200 -0.31700 #END
当前使用的Python脚本
import re from pathlib import Path import csv writer = None with open("out.csv", "w") as csvfile: for f in Path('.').glob("*.cif"): print(f) with open(f, "r") as fin: row = {} for line in fin: line = line.strip() if len(line) == 0 or line.startswith("#"): # skip blank lines and comments continue # this matches lines like "_cell_length_a 9.316(3)" # and splits the input into 2 variables. # also ignore lines like "_symmetry_equiv_pos_site_id" if m := re.match(r"_?(\w+)\s+(.*)", line): key = m.group(1) value = m.group(2) if key.startswith("cell_length") or key.startswith("cell_angle"): if m := re.match(r"([^)]+)\(([^)]+)\)", value): # split off the uncertainty value = m.group(1) unc = m.group(2) # add uncertainty as separate column row[key + "_unc"] = unc row["filename"] = f row[key] = value print(row) if writer is None: # this is the first row found # create the .csv file with column names # from the keys in the .cif file (e.g. _cell_length_a) writer = csv.DictWriter(csvfile, fieldnames=sorted(row.keys())) writer.writeheader() writer.writerow(row)
遇到的错误
执行脚本时触发如下ValueError:
Traceback (most recent call last): File "C:\Users\Owner\CSD_API\unit_cell.py", line 39, in <module> writer.writerow(row) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\csv.py", line 154, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\csv.py", line 149, in _dict_to_list raise ValueError("dict contains fields not in fieldnames: " ValueError: dict contains fields not in fieldnames: 'cell_angle_alpha_unc', 'cell_angle_gamma_unc'
问题
如何修改脚本,避免因CIF文件布局不同导致的字段不匹配错误,成功提取数据至CSV?
内容的提问来源于stack exchange,提问作者matsmcfly
相关产品推荐
相关产品推荐

