'How to parse an XYZ file into 3 separate files based on Z value?

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']


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source