Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

     print textfile_temp.split('------------|------------|--------|--------|----------|-----------|')[1].split("------------|------------|--------|--------|----------|-----------|")[0]

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading