Let’s learn how to convert Numpy to an XYZ file. We will use the Numpy reshape method for that purpose.
Having a file means you can format it as a Numpy array using existing functions.
Converting Numpy to xyz
To get the xyz file format, it is enough to use the simple reshape method with -1 as a parameter.
import numpy as np my_file = np.genfromtxt('C:/Users/Pythoneo/Documents/veryimportantfile.txt', dtype='str') print(f"My file \n{my_file}") my_array = np.array(my_file) my_array = my_array.reshape(-1, 3) print(f"My xyz array \n{my_array}")
As you may have noticed, reshape(-1, 3) created a three-column array. Thanks to the -1 parameter, Numpy will adjust the row numbers by themselves.