Writing and reading complex numbers using numpy.savetxt and numpy.loadtxt

大城市里の小女人 提交于 2019-12-01 20:46:21

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:

import numpy

d1 = -0.240921619563-0.0303165074169j
d2 = -0.340921619563-0.0403165074169j
d3 = -0.440921619563-0.0503165074169j
d4 = -0.540921619563-0.0603165074169j

array = 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!