The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Question
00:59 Accepted answer (Score 3)
01:59 Answer 2 (Score 5)
02:16 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.