python: grab a specific line after a match
<p>I have have df full of text<br>
I would like to define variable <strong>date</strong> which is always three lines after <strong>version</strong>.
here is my code</p>

<pre><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)
</code></pre>

<p>but it does not work for the <strong>date</strong> variable and i receive the following error. Can anybody help?</p>

<pre><code>TypeError: Can't convert 'int' object to str implicitly
</code></pre>

<p>What the error message is telling you is that you cannot add a number to a string.</p>

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

<p>Check out the <a href="https://docs.python.org/2/library/datetime.html" rel="nofollow">datetime documentation here</a>.</p>

<p>In your case, you might want to try something like:</p>

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

<p>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.</p>

python loops lines