Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

python reading lines

python reading lines

python reading lines


I have read a file. Now I have all these lines of strings. I need to extract specific lines. These lines are between two lines with a specific string in them. There is no set amount of lines between them. Also, I have to match the two lines. For example, There may be the string in the first line, but there is no match, so I will get the same string after a number of lines. Then I have to see if that line has the match. I have no clue how to do this.

def main():
infile = open(“file.txt”, “r”)
outfile = open(outfile.txt”,”w”)
for line in infile:
if “theString” in line:
for line in infile:
if “different string” or “anotherString” in line:

RE: python reading lines


As indentation is mandatory in Python, please start a new thread where you post your code between [code] and [/code] TGML tags to preserve formatting.

Then click the Inappropriate post? If so, Red Flag it! at the bottom of your starting post here and ask forum management to delete this thread as you already posted a cleaned up version of it.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close