Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have some data files that contain boolean output from Fortran code:

write(23,'(L2)') data

therefore, a portion of the file will look like this:

F F T F ...

I would like to read this file in Python with numpy.asarray() function, because it is easy to convert data this way, e.g.:

data = asarray(f.readline().split(),'bool')

However, no matter what data it is, Python always returns an array with all 'True's.

I have also tried to write as 'False False True False ...' or '0 0 1 0 ...', and they both did not work.

I would like to know if there is a way to use asarray() to achieve this? or any other suggestions that can convert boolean data without using loops?

like image 618
warriormole Avatar asked Jul 23 '26 14:07

warriormole


1 Answers

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)
like image 118
Greg Avatar answered Jul 26 '26 05:07

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!