I have an XYZ file in the following format:
X[m] | Y[m] | DensA_1050c[m] | DensB_1200c[m] | DensC_1250c[m] |
---|---|---|---|---|
627841.54 | 231758.7 | 12.77 | 12.98 | 13.17 |
627841.54 | 231758.7 | 12.77 | 12.98 | 13.17 |
627841.54 | 231758.7 | 12.77 | 12.98 | 13.17 |
627841.54 | 231758.7 | 12.77 | 12.98 | 13.17 |
I am looking for a way to read in the XYZ file in python and then re-write the XYZ file into 3 separate XYZ files like such:
DensA_1050c[m]
X[m] | Y[m] | Z[m] |
---|---|---|
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[m]
X[m] | Y[m] | Z[m] |
---|---|---|
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[m]
X[m] | Y[m] | Z[m] |
---|---|---|
627841.54 | 231758.7 | 13.17 |
627841.54 | 231758.7 | 13.17 |
627841.54 | 231758.7 | 13.17 |
627841.54 | 231758.7 | 13.17 |
I've tried the following code to read in the XYZ file which works, but I don't know how to parse it to be like the above examples.
import numpy as np
file_location = 'C:/Users/Public/AllData.xyz'
xyz_file = np.genfromtxt(fname=file_location, skip_header=2, dtype='unicode')
print(xyz_file)
The result from the above code is:
['627201.81' '233336.97' '12.94' '13.27' '13.41']
The following code is very closely tailored to your problem. The row headers which are used for the output file names are hard-coded and mapped to the corresponding column index.
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