The Python Oracle

how to read in boolean data file in Python with asarray function of numpy

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 How To Read In Boolean Data File In Python With Asarray Function Of Numpy
00:52 Accepted Answer Score 3
01:37 Answer 2 Score 5
01:49 Thank you

--

Full question
https://stackoverflow.com/questions/1838...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #arrays #numpy #boolean

#avk47



ANSWER 1

Score 5


If you can read in the data as an array of "T" and "F" strings then you could do the following:

>>> a = np.array(["T", "F", "T"])
>>> a == "T"
array([ True, False,  True], dtype=bool)



ACCEPTED ANSWER

Score 3


One way would be to do a tiny bit more processing on each of the strings of the input file to get the results you're expecting:

with open('input.dat') as handle:
    data = asarray([[x == 'T' for x in line.strip().split()]
                    for line in handle],
                   dtype=bool)

Here I'm reading through each line in the file handle, and then for each field on the line, I compare it to the string 'T'. This will give a boolean result, which can be stored in your data array as expected.

The problem with the code you presented is that Python is doing its best to convert the values that you give it to Booleans; however, in Python non-empty strings evaluate to True when cast as Booleans: bool('T') == bool('F') == bool('0') == bool('1') == True.