Search code examples
pythonloopslines

python: grab a specific line after a match


I have have df full of text
I would like to define variable date which is always three lines after version. here is my code

with open(input_file1,'r') as f:
    for i, line in enumerate(f):
        if line.startswith('Version'):
            version = line.strip()
            date = line + 3
            print(line,date)

but it does not work for the date variable and i receive the following error. Can anybody help?

TypeError: Can't convert 'int' object to str implicitly

Solution

  • line is the string contents of the line in the file. i is the incrementally increasing index of the line being parsed. Therefore you want to read line number i+3. You can read three lines ahead easily if you read all the lines into memory with .readlines().
    NOTE this is not advisable if your files are very large!

    with open(input_file1,'r') as f:
        lines = f.readlines()
        for i, line in enumerate(lines):
            if line.startswith('Version'):
                version = line.strip()
                date = lines[i + 3].strip()
                print(line,date)