问题描述
stackoverflow,
stackoverflow,
我有一个包含复数的矩阵(例如-2.2982235934153075E-11 + 2.1179547211742553E-9i),我需要导入到numpy数组。我一直使用 genfromtext(file)
来解析所有我的其他真实值,但我得到一个 nan
为所有复数值。有任何想法吗?
I have a matrix containing complex numbers (ex. -2.2982235934153075E-11+2.1179547211742553E-9i) that I need to import to a numpy array. I've been using genfromtext(file)
to parse all my other, real values, but I'm getting a nan
for all complex values. Any ideas?
self.raw = (genfromtxt(self.loc, delimiter=',', skip_header=9, dtype=float))
[m,n] = shape(self.raw)
data = zeros((m, n-3))
data[:, :] = self.raw[:, 3::]
返回:
data = array([nan, nan, nan, ...])
推荐答案
我最终必须这样做的方式是首先 replace('i','j')
单元格,并保存新的,已更正的文件。之后,用 dtype = str
读取.csv会在后续计算中导致错误,但结果是您可以使用 dtype = complex128
,这解决了我所有的问题。感谢您对Saullo-Castro
The way I ended up having to do this was to first replace('i', 'j')
for all cells in the original .csv file and save the new, corrected file. Afterwards, reading the .csv with dtype=str
caused errors in subsequent calculations, but it turns out you can parse the .csv with dtype=complex128
, which solved all my problems. Thanks for the help on the conversion @Saullo-Castro
这篇关于numpy用复数读取.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!