python loadtxt with exotic format of table
<p>I have a file from simulation which reads like :</p>

<pre><code>5.2000 -0.01047 -0.02721 0.823400 -0.56669 1.086e-5 2.109e-5 -1.57e-5 -3.12e-5
       0.823400 -0.56669 -0.02166 -0.01949 -2.28e-5 -2.66e-5 1.435e-5 1.875e-5
       1.086e-5 2.109e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01836 0.820753 -0.57065
       -1.57e-5 -3.12e-5 1.435e-5 1.875e-5 0.820753 -0.57065 -0.01066 -0.02402
5.2005 -0.01045 -0.02721 0.823354 -0.56676 1.086e-5 2.109e-5 -1.57e-5 -3.12e-5
       0.823354 -0.56676 -0.02167 -0.01947 -2.28e-5 -2.66e-5 1.435e-5 1.875e-5
       1.086e-5 2.109e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01833 0.820703 -0.57073
       -1.57e-5 -3.12e-5 1.435e-5 1.875e-5 0.820703 -0.57073 -0.01063 -0.02401
5.2010 -0.01043 -0.02721 0.823309 -0.56683 1.087e-5 2.108e-5 -1.57e-5 -3.12e-5
       0.823309 -0.56683 -0.02168 -0.01945 -2.28e-5 -2.66e-5 1.435e-5 1.874e-5
       1.087e-5 2.108e-5 -2.28e-5 -2.66e-5 -0.01878 -0.01830 0.820654 -0.57080
       -1.57e-5 -3.12e-5 1.435e-5 1.874e-5 0.820654 -0.57080 -0.01061 -0.02400
</code></pre>

<p>And I would like to get it as a float + an array of float (the float would be the '5.2000' and the array what is after (4x8 table)
but the numpy command loadtxt don't get this exotic kind of structure. Is there a solution to it ?</p>

<p>If the "tables" are always 4x8 then it may be easier to read the data in as a 1D array, then index/reshape this in order to get the output you desire:</p>

<pre><code># to get s you could do something like s = open(fname, 'r').read()
s = """
5.2000 -0.01047  -0.02721   0.8234   -0.56669   1.086e-5  2.109e-5 -1.57e-5  -3.12e-5
        0.8234   -0.56669  -0.02166  -0.01949  -2.28e-5  -2.66e-5   1.435e-5  1.875e-5
        1.086e-5  2.109e-5 -2.28e-5  -2.66e-5  -0.01878  -0.01836   0.820753 -0.57065
       -1.57e-5  -3.12e-5   1.435e-5  1.875e-5  0.820753 -0.57065  -0.01066  -0.02402
5.2005 -0.01045  -0.02721   0.823354 -0.56676   1.086e-5  2.109e-5 -1.57e-5  -3.12e-5
        0.823354 -0.56676  -0.02167  -0.01947  -2.28e-5  -2.66e-5   1.435e-5  1.875e-5
        1.086e-5  2.109e-5 -2.28e-5  -2.66e-5  -0.01878  -0.01833   0.820703 -0.57073
       -1.57e-5  -3.12e-5   1.435e-5  1.875e-5  0.820703 -0.57073  -0.01063  -0.02401
5.2010 -0.01043  -0.02721   0.823309 -0.56683   1.087e-5  2.108e-5 -1.57e-5  -3.12e-5
        0.823309 -0.56683  -0.02168  -0.01945  -2.28e-5  -2.66e-5   1.435e-5  1.874e-5
        1.087e-5  2.108e-5 -2.28e-5  -2.66e-5  -0.01878  -0.0183    0.820654 -0.57080
       -1.57e-5  -3.12e-5   1.435e-5  1.874e-5  0.820654 -0.5708   -0.01061  -0.02400
"""

# a 1D array of floats
x = np.array(s.split(), dtype=np.double)

# we can extract the first column by indexing every 33rd element, since each "section"
# contains one float in the left-hand column and 4*8 = 32 values in the "table".
first_col = x[::33]

# we can extract the values corresponding to the "tables" by constructing a boolean
# vector that is True wherever the index is not divisible by 33
tables = x[(np.arange(x.size) % 33) &gt; 0]

# finally we can reshape these values to get an array of 4x8 tables stacked in the
# first dimension
tables = tables.reshape(-1, 4, 8)

print(repr(first_col))
# array([ 5.2   ,  5.2005,  5.201 ])

print(repr(tables[0]))
# array([[ -1.04700000e-02,  -2.72100000e-02,   8.23400000e-01,
#          -5.66690000e-01,   1.08600000e-05,   2.10900000e-05,
#          -1.57000000e-05,  -3.12000000e-05],
#        [  8.23400000e-01,  -5.66690000e-01,  -2.16600000e-02,
#          -1.94900000e-02,  -2.28000000e-05,  -2.66000000e-05,
#           1.43500000e-05,   1.87500000e-05],
#        [  1.08600000e-05,   2.10900000e-05,  -2.28000000e-05,
#          -2.66000000e-05,  -1.87800000e-02,  -1.83600000e-02,
#           8.20753000e-01,  -5.70650000e-01],
#        [ -1.57000000e-05,  -3.12000000e-05,   1.43500000e-05,
#           1.87500000e-05,   8.20753000e-01,  -5.70650000e-01,
#          -1.06600000e-02,  -2.40200000e-02]])
</code></pre>

python numpy