Reputation: 281
I am trying to read a file and create an array from it. The file is as follows:
1 0
0 1
The code is:
line = file.read()
array = np.fromstring(line.strip(),dtype = bool, sep = " ")
array.resize(2,2)
print array
The output is:
[[ True False]
[False True]]
but there is always an extra space before 'True.' Does anyone know how to remove it?
Upvotes: 0
Views: 107
Reputation: 7842
You have reproduced the output incorrectly:
In [8]: print np.fromstring(line,sep = " ").reshape(2,2).astype("bool")
[[ True False]
[False True]]
The values are right-aligned for each column.
As an aside, the more numpythonic way of doing this is:
In [9]: np.genfromtxt("<name of text file>").astype("bool")
Out[9]:
array([[ True, False],
[False, True]], dtype=bool)
Upvotes: 1