Numpy array as txt file with specific syntax

Viewed 27

Right now I am using the following to save a txt file

np.savetxt(data.txt, data)

When I open the txt file the output looks something like the following:

1.000000000000000000e+00 0.000000000000000000e+00 1.169886320011951995e+02
2.000000000000000000e+00 0.000000000000000000e+00 1.622808196685386122e+02
3.000000000000000000e+00 0.000000000000000000e+00 1.167895805540496781e+02

But I need the output to look the following to be able to use it in another program. I.e the first two columns are just saved as integers and the last column should have no more than 8 decimal points.

1 0 1.16988632e+02
2 0 1.62280819e+02
3 0 1.16789580e+02
1 Answers

You need to use fmt parameter. Try this:

np.savetxt('data.txt', data, fmt=['%d', '%d', '%1.8e'])
Related