UserYmY
UserYmY

Reputation: 8554

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

Upvotes: 0

Views: 1578

Answers (2)

fp1991
fp1991

Reputation: 140

What the error message is telling you is that you cannot add a number to a string.

You first need to convert the string to a datetime object, to which you can then add anything you want.

Check out the datetime documentation here.

In your case, you might want to try something like:

import datetime
Date = datetime.datetime.strptime(line, "%m/%d/%Y")

Where the second set of arguments determine the format of the date you are feeding the datetime object via your line variable. You will have to change this bit ("%m/%d/%Y") to match your input.

Upvotes: 0

Jdog
Jdog

Reputation: 10721

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)

Upvotes: 1

Related Questions