我知道如果我想用 numpy 保存和加载复数数组,我可以使用此处描述的方法:How to save and load an array of complex numbers using numpy.savetxt? .
然而,假设有人不知道这一点并使用 numpy.savetxt("numbers.txt",numbers
) 保存了他们的数组 numbers
,生成了一个文件表单条目
(0.000000000000000000e+00+-2.691033635430225765e-02j) .
在这种情况下
numbers_load = numpy.loadtxt("numbers.txt").view(complex)
可以预见,在某种意义上会失败
ValueError: could not convert string to float: (0.000000000000000000e+00+-2.691033635430225765e-02j) .
从这个文件中提取复数的简单方法是什么(而不生成它的不同版本)?
最佳答案
指定数据类型,如:
np.loadtxt("numbers.txt", dtype=np.complex_)
对我有用。
关于python - 使用 numpy.loadtxt 加载复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348824/