Printing Line(s) After Specific String From txt

This is the place for queries that don't fit in any of the other categories.

Printing Line(s) After Specific String From txt

Postby ohtbv » Wed May 01, 2013 7:14 pm


I have a Text File with a whole array of information. My idea is to find the line with the string I am looking for, and then print the line that comes after that.

For example:

Movement speed
$550.70 USD

I would search for "Total", and then print the line after "Total" which would be "$550.70 USD". This constantly repeats for the other units and I need to print them all so I assume it would be a "for loop"

So far I have thought of either:

Code: Select all
file = open(FileName.txt)
for line in file:
    if str == 'Total':
        print (line + 1)


Code: Select all
x = False
file = open("FileName.txt")
captureline = ""

for line in file
   if line = "Total"
        x = True
   if x = True
       captureline = line

Both are more guidelines of the structure since I am not too sure on how to actually get it to work.

Posts: 1
Joined: Wed May 01, 2013 6:56 pm

Re: Printing Line(s) After Specific String From txt

Postby Yoriz » Wed May 01, 2013 8:11 pm

Have a look at KevinD's post in the following link
Due to the reasons discussed here we will be moving to on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1671
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 10 guests