Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

python reading lines

Status
Not open for further replies.

berrybub

Programmer
Mar 11, 2013
2
US


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:
break
for line in infile:
if “different string” or “anotherString” in line:
break
outfile.write(line)

 
Hi

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

Then click the [navy]Inappropriate post? If so, Red Flag it![/navy] 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.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top