Skip to content Skip to sidebar Skip to footer

Writing And Reading Complex Numbers Using Numpy.savetxt And Numpy.loadtxt

I need to write and read complex numbers. I would like to use numpy.savetxt and numpy.loadtxt to do so. Since the code that I have written is rather big I created a test file to tr

Solution 1:

Thanks Warren Weckesser! The link you suggested helped me a lot. I now have two working scripts: one for writing complex numbers using numpy.savetxt and one for reading/loading the complex numbers from the file using numpy.loadtxt.

For future references, the codes are listed below.

Writing:

importnumpyd1= -0.240921619563-0.0303165074169jd2= -0.340921619563-0.0403165074169jd3= -0.440921619563-0.0503165074169jd4= -0.540921619563-0.0603165074169jarray= numpy.array([d1, d2, d3, d4])

save = open("test.dat","w")
numpy.savetxt(save, array.reshape(1, array.shape[0]), newline = "\r\n", fmt = '%.4f%+.4fj '*4)

save.close()

Reading/Loading:

import numpy

coeffs = numpy.loadtxt("test.dat", dtype = numpy.complex128)

Post a Comment for "Writing And Reading Complex Numbers Using Numpy.savetxt And Numpy.loadtxt"