1

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

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]))

But the datatype of the negative value still shows float64:

The screenshot of code and output.

I also tried reading the values as a list and then converting to numpy array but that also didn't work.

Is there any other way I can read negative values as a numpy array from a file input?

MBT
  • 21,733
  • 19
  • 84
  • 102
Ashutosh Kumar
  • 109
  • 1
  • 9
  • 3
    Please do not post text such as code, error messages, or data on SO, always post the text directly here on SO. – Mr. T Sep 09 '18 at 06:50

2 Answers2

4

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

Try this code instead:

data=pd.read_csv("data.txt",delimiter=",")
data = DataFrame(data)
Abo Omar
  • 125
  • 1
  • 10