0

Hi,
I have the following problem: I would like to read from a file between to keywords (KEY1 and a line consisting of ------), skipping 4 lines after the first keyword. What I have is the following:

import re
import string

infileName = raw_input("filename ")
infile = open(infileName, 'r')
data = infile.readlines()

counter = True

for index, line in enumerate(data):
    if re.search("KEY1:", line):
       index += 4
       f = 0
       while counter:
           f = f + 1
           print string.split(data[f+index])
           if re.search(str([--]), string.split(line)):
               counter = False

Has anyone suggestions how I can fix this code? Apparently the re.search doesn't work in the while look and how can I just check for two '--' and break the loop instead searching for the whole line

Thanks,
awa

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by awa
0

The following code is a bit cleaner

import re

infileName = raw_input("filename ")
infile = open(infileName, 'r')
data = infile.readlines()

printLines = False
skipCount  = 0
for index, line in enumerate(data):
    if "KEY1:" in line:
        printLines = True
        skipCount = 4
        continue
    if "---" in line:
        printLines = False
        continue
    if printLines:
        if skipCount>0:
            skipCount -= 1
            continue
        print line,  # , to prevent a newline character

Edited by djidjadji: n/a

0

works for problem as described but mine was more complicated in the end than the one I posted but I could fix it :-)
thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.