如何根据Z值将XYZ文件解析为3个独立的文件



我有一个XYZ文件,格式如下:

12.9813.17<12.77><22.98>>13.17<12.77><22.98>>13.17<12.77><22.98>>13.17
X[m][th> Y[m][th> DensA_1050c[m][th] DensB_1200c[m][th> DensC_1250c[m][st>
627841.54 231758.7 12.77
627841.54 231758.7
627841.54 231758.7
627841.54 231758.7

下面的代码非常适合您的问题。用于输出文件名的行标题经过硬编码并映射到相应的列索引。

import numpy as np
file_location = 'AllData.xyz'
xyz_file = np.genfromtxt(fname=file_location, skip_header=2, dtype='unicode')
mappings = { 'DensA_1050c.xyz': 2, 'DensB_1200c.xyz' : 3, 'DensC_1250c.xyz' : 4 }
for mapping in mappings:
with open(mapping, 'w') as output_file:
for record in xyz_file:
output_file.write(record[0])
output_file.write('t')
output_file.write(record[1])
output_file.write('t')
output_file.write(record[mappings[mapping]])
output_file.write('n')

DensA_1050c.xyz

627841.54   231758.7    12.77
627841.54   231758.7    12.77
627841.54   231758.7    12.77
627841.54   231758.7    12.77

DensB_1200c.xyz

627841.54   231758.7    12.98
627841.54   231758.7    12.98
627841.54   231758.7    12.98
627841.54   231758.7    12.98

DensC_1250c.xyz

627841.54   231758.7    13.17
627841.54   231758.7    13.17
627841.54   231758.7    13.17
627841.54   231758.7    13.17

相关内容