-2

I am attempting to save an array to a txt file, I used the following function:

numpy.savetxt('C:/Users/Adminstrator/Desktop/mesh/ELIST2-clean.txt',array2DClean, delimiter='\t')

The data in file are shown as following:

1.300000000000000000e+01    2.710000000000000000e+02    2.360000000000000000e+02    7.200000000000000000e+01    2.350000000000000000e+02
2.400000000000000000e+01    2.760000000000000000e+02    2.060000000000000000e+02    1.310000000000000000e+02    1.300000000000000000e+02
3.200000000000000000e+01    2.580000000000000000e+02    2.820000000000000000e+02    2.570000000000000000e+02    5.000000000000000000e+01
3.600000000000000000e+01    2.800000000000000000e+02    5.100000000000000000e+01    5.000000000000000000e+01    1.030000000000000000e+02
3.900000000000000000e+01    2.800000000000000000e+02    2.250000000000000000e+02    1.120000000000000000e+02    1.110000000000000000e+02
4.300000000000000000e+01    2.810000000000000000e+02    1.630000000000000000e+02    2.200000000000000000e+01    1.640000000000000000e+02
4.900000000000000000e+01    2.850000000000000000e+02    1.150000000000000000e+02    1.600000000000000000e+02    1.610000000000000000e+02

How can I format the numbers written to the file as whole integers without xe+y notation?

1 Answers1

0

numpy.savetxt has an argument fmt that takes the number format.

fmt: str or sequence of strs, optional

A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored. For complex X, the legal options for fmt are:

a single specifier, fmt=’%.4e’, resulting in numbers formatted like ‘ (%s+%sj)’ % (fmt, fmt)

a full string specifying every real and imaginary part, e.g. ‘ %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej’ for 3 columns

a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columns

Do numpy.savetxt('C:/Users/Adminstrator/Desktop/mesh/ELIST2-clean.txt',array2DClean, delimiter='\t', fmt='%.0f') to use a format string that writes floats out in fixed-point notation with zero decimal places.

More info here: https://docs.python.org/3/library/string.html#format-specification-mini-language

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70