mardi 20 février 2018

Python Regex - If Statement equal to 'string' and match entire line

I have a script that prints all the lines between header lines, but it only works with my dummy data, so I am trying to match the entire line after a certain string.

Say I have this data in a text file:

Dumb Data

index1 0000

random data

index1 0000

random data

index2 0000

The script is able to extract all lines after index 1 and stop before index 2, but it only works if the index line matches extactly in my if statment. Meaning if I delete everything after "index1" it works just fine. So I attempted to use regular expressions, based off reading the python documentation here https://docs.python.org/3.4/library/re.html.

Here is my code: It works withtout the regular expressions parts, and if I swap out result1 and result2 with the extract strings I need. What am I missing? I use the '.' to match everything after 'index1', correct?

import re

myvar = False
prog = re.compile('.')
result1 = prog.match('index1')
result2 = prog.match('index2')

with open('Sample Test.txt') as f:
    for line in f:
        if result1 in line:
            myvar = True
            print (line)

        elif result2 in line:
            myvar = False
            print (line)

        elif myvar == True:
            print(line)
            continue

I get this traceback when I try running it:

Traceback (most recent call last): File "C:\mytest\test.py", line 10, in if result1 in line: TypeError: 'in ' requires string as left > operand, not _sre.SRE_Match

Aucun commentaire:

Enregistrer un commentaire