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

Hello,

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
35
Defense
22
Total
$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)


or

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

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


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

Thanks!
ohtbv
 
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
http://www.python-forum.org/viewtopic.php?f=25&t=197&p=1330#p1330
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests