title: How can I efficiently load this kind of ASCII files with python?
body: <p>I have big fortran-generated ASCII files formatted like this:</p>
  
  <pre><code>x y z  num_line index
  1 float
  2 float
  ...
  num_line float
  x2 y2 z2 num_line2 index2
  1 float
  2 float
  ...
  num_line2 float
  ...
  </code></pre>
  
  <p>The number of blocks can go up to thousands and the number of lines whithin each block to hundreds.</p>
  
  <p>Let's give an example of what I get:</p>
  
  <pre><code>0.0 0.0 0.0  4 0
  1 0.5
  2 0.9
  3 0.4
  4 0.1
  0.0 0.0 1.0  4 1
  1 0.2
  2 0.2
  3 0.4
  4 0.9
  0.0 1.0 2.0  5 2
  1 0.7
  2 0.6
  3 0.9
  4 0.2
  5 0.7
  </code></pre>
  
  <p>And what I want from that (as a numpy matrix):</p>
  
  <pre><code>0.5 0.2 0.7
  0.9 0.2 0.6
  0.4 0.4 0.9
  0.1 0.9 0.2
  nan nan 0.7
  </code></pre>
  
  <p>Of course, I can use:</p>
  
  <pre><code>my_mat = []
  with open("myfile", "r") as f_in:
      niter = int(f_in.readline().split()[3])
      while niter:
          curr_vect = zeros(niter)
          for i in xrange(niter):
              curr_vect[i] = float(f_in.readline().split()[1])
          my_mat.append(curr_vect)
          line = f_in.readline()
          if line is not None:
              niter = int(line.split()[3])
          else:
              niter = False
  my_mat = array(my_mat)
  </code></pre>
  
  <p>The problem is this is not really efficient and too much complicated for what it is. I already know about numpy's <code>loadtxt</code> and <code>genfromtxt</code> but they do not seem to apply there.</p>
  
  <p>I'm looking for something faster and much more readable. Any idea?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Please forgive me, my question was not complete and some of you lost his time because of me. Here is a real example of such a block:</p>
  
  <pre><code>3.571428571429E-02 3.571428571429E-02-3.571428571429E-02         1   35  
         1 -0.493775207966779     
         2  0.370269037864060     
         3  0.382332033744703     
         4  0.382332033744703     
         5  0.575515346181205     
         6  0.575515346181216     
         7  0.575562530624028     
         8  0.639458035564442     
         9  0.948445367602052     
        10  0.948445367602052     
        11  0.975303238888803     
        12   1.20634795229899     
        13   1.21972845646758     
        14   1.21972845646759     
        15   1.52659950368213     
        16   2.07381346028515     
        17   2.07629743909555     
        18   2.07629743909555     
        19   2.15941179949552     
        20   2.15941179949552     
        21   2.30814240005132     
        22   2.30814240005133     
        23   2.31322868361483     
        24   2.53625115348660     
        25   2.55301153157825     
        26   2.55301153157826     
        27   2.97152031842301     
        28   2.98866790318661     
        29   2.98866790318662     
        30   3.24757159459268     
        31   3.27186643004142     
        32   3.27186643004143     
        33   3.37632477135298     
        34   3.37632477135299     
        35   3.55393884607834     
  </code></pre>