2

I would like to read many numbers from a Gaussian formatted checkpoint file. The corresponding part looks like this:

 Cartesian Force Constants                  R   N=       17391
   6.82119997E-01  1.28823797E-01  4.64548230E-01  8.16802733E-02 -2.63533593E-01
   4.30400615E-01 -1.41139143E-01  4.62611793E-02 -7.32145345E-02  6.16655830E-01
   ...

N number means I have to read 17391 doubles, which are in 3479 lines. The file is too huge to read all of the lines, I look for a smarter solution.

What I have tried so far: I searched the expression with a for loop

fchk = open(filepath,'r')
for line in fchk:
    if "Cartesian Force Constants" in line:
        header = line
        count = int(header[50:62])
        Elements = np.empty([count],dtype=np.float_)

After this if I try read more line inside or outside the previous for loop I got

ValueError: Mixing iteration and read methods would lose data
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Berta Dénes
  • 93
  • 3
  • 11
  • Please provide the full code when you re-read the file. And provide full traceback. – Paolo Casciello May 18 '14 at 09:28
  • It's the `for line in fchk` loop that does this; it's the same problem as the duplicate question I closed this against. Use `for line in iter(fckh.readline, ''):` to avoid using the iteration buffer. – Martijn Pieters May 18 '14 at 09:31
  • @PaoloCasciello: in this case the exception would not clearly pinpoint the problem. However, the code posted and the exception text together are more than enough to diagnose here. – Martijn Pieters May 18 '14 at 09:32
  • @MartijnPieters oh nice. i was expecting instead something like a call to `.read` after that `for`. :) – Paolo Casciello May 18 '14 at 09:40

0 Answers0