Suppose I have the following txt file:
0.0163934
6
7.52438e+09
2147483648
6.3002e-06 6.31527e-08 0 0 6 0 0 4.68498e-06 0.00638412 12.6688
6.33438e-06 0 5.99588e-09 0 0 0 0 4.70195e-06 0 12.876
6.36874e-06 0 6.09398e-09 0 0 0 0 4.71894e-06 0 13.0867
6.40329e-06 0 6.19369e-09 0 0 0 0 4.73593e-06 0 13.3009
6.43802e-06 0 6.29503e-09 0 0 0 0 4.75294e-06 0 13.5185
6.47295e-06 0 6.39803e-09 0 0 0 0 4.76996e-06 0 13.7397
0.0163934
3
7.52438e+09
2147483648
6.3002e-06 0 5.89935e-09 0 0 0 0 4.68498e-06 0 12.6688
6.33438e-06 0 5.99588e-09 0 0 0 0 4.70195e-06 0 12.876
6.36874e-06 0 6.09398e-09 0 0 0 0 4.71894e-06 0 13.0867
I want to read each of the first lines as floats or integers and then depending on the second line I want to read the rest of lines as a list of lists or array.
In IDL language I just have to do:
openr, 1, fname
readf, 1, Time
readf, 1, Bins
readf, 1, dummy
readf, 1, dummyLong
da1= fltarr(10, Bins)
readf, 1, da1
So that the entire block of numbers is stored in the integer da1 which is size: 10*Bins. (rows and columns are the opposite as in python)
And then I can read the following lines in the same way.
In python I am doing:
Time=float(filen.readline())
Bins=int(filen.readline())
dummy=float(filen.readline())
dummyLong=long(filen.readline())
lines=[filen.readline() for i in range(Bins)]
arra=[[float(x) for x in lines[i].split()] for i in range(len(lines))]
So I need two lines of code and complicated iterations that are not understandable to a beginner.
Is there a way to do it like in IDL, in a single statement and pythonic?
Thanks!