numPy在从文件读取负数时给出nan [英] numPy gives nan while reading a negative number from a file
问题描述
我试图读取存储在具有以下值的文本文件中的3x3矩阵的内容.
I tried to read the contents of a 3x3 matrix stored in a text file having the following values.
−30 10 20
10 40 −50
20 −50 −10
我按如下方式使用numpy.genfromtxt
,但是当它给出nan
代替负数据值时.
I used numpy.genfromtxt
as follows but when it gave nan
in place of negative data values.
data = np.genfromtxt('file.txt',delimiter=' ')
print(data)
print(type(data[0][0]))
但是负值的数据类型仍然显示float64
:
But the datatype of the negative value still shows float64
:
我还尝试将这些值作为列表读取,然后转换为numpy数组,但这也不起作用.
I also tried reading the values as a list and then converting to numpy array but that also didn't work.
还有其他方法可以从文件输入中读取负值作为numpy数组吗?
Is there any other way I can read negative values as a numpy array from a file input?
推荐答案
您的文件中包含U + 2212减号字符,而不是U + 002D HYPHEN-MINUS人们通常认为的减号字符. genfromtxt
不处理该字符.在尝试解析数据之前,请用连字符-减号替换减号字符.
You've got U+2212 MINUS SIGN characters in your file, not the U+002D HYPHEN-MINUS people usually think of as the minus sign character. genfromtxt
doesn't handle that character. Replace the minus sign characters with hyphen-minuses before trying to parse the data.
这篇关于numPy在从文件读取负数时给出nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!