Read FILE.txt, then extract part between two lines using Python

Question:

I have a FILE.txt file, which contains around 3000 lines.
I wanted to extract several lines (A TABLE that contains 10 columns). So that I can subsequently draw a graph which shows these results in the table

  • I tried with this simple code but an error is generated "SyntaxError: invalid syntax"

FILE.txt

            |  Sim. Time |  t_SCF | t_Grad |     Temp |     E_Kin |
       Step |       [fs] |    [s] |    [s] |      [K] | [Hartree] |
------------|------------|--------|--------|----------|-----------|
          0          0.0    103.5              300.00    0.065553
          1          1.0     87.5     26.7     292.77    0.063973
          2          2.0     89.5     25.6     274.46    0.059971
       1996       1996.0     93.1     24.7     315.92    0.222478
       1997       1997.0     91.8     24.9     312.40    0.068263  
       1998       1998.0    103.1     24.9     310.47    0.067841 
       1999       1999.0     97.7     25.6     310.42    0.067830 
       2000       2000.0     98.4     24.8     309.65    0.067661 
------------|------------|--------|--------|----------|-----------|
            |  Sim. Time |  t_SCF | t_Grad |     Temp |     E_Kin |
       Step |       [fs] |    [s] |    [s] |      [K] | [Hartree] |

my script:

with open('FILE.txt', 'rb') as f:
     textfile_temp = f.read()

     print textfile_temp.split('------------|------------|--------|--------|----------|-----------|')[1].split("------------|------------|--------|--------|----------|-----------|")[0]
Asked By: al ahmed

||

Answers:

I’d just go with a simple state machine. (Note 'r' mode instead of 'rb'!)

with open('FILE.txt', 'r') as f:
    in_block = False
    for line in f:
        if line.startswith("------------"):
            if in_block:  # trailing separator, we can stop here
                break
            in_block = True
            continue
        if in_block:
            print(line.split())

This prints out

['0', '0.0', '103.5', '300.00', '0.065553']
['1', '1.0', '87.5', '26.7', '292.77', '0.063973']
['2', '2.0', '89.5', '25.6', '274.46', '0.059971']
['1996', '1996.0', '93.1', '24.7', '315.92', '0.222478']
['1997', '1997.0', '91.8', '24.9', '312.40', '0.068263']
['1998', '1998.0', '103.1', '24.9', '310.47', '0.067841']
['1999', '1999.0', '97.7', '25.6', '310.42', '0.067830']
['2000', '2000.0', '98.4', '24.8', '309.65', '0.067661']

for your data.

Answered By: AKX
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.