Ripseed Oil
Ripseed Oil

Reputation: 77

How to print the next line from a text file python

I am trying to print two lines of text from a text file in python which are next to eachother on a separate line. So the text file looks like this:

Apples
Oranges
Pears
Lemons

And if someone inputs Apples I would like the program to print out:

Apples
Oranges

Here is the code I have so far:

file = open('Fruit.txt',"r")
for line in file:
    if InputText in line:
        print(line)
        print(line+1)
        return
file.close() 

Of course the variable line+1 isn't correct but I have left it there to illustrate that the next line of text must also be printed.

Upvotes: 4

Views: 14835

Answers (2)

AlanK
AlanK

Reputation: 9853

If your line matches you can call next(input) to generate the following line. Also if you use the with context manager, you remove the need to close the file and this will clean up the code a little bit

InputText = 'Pears'

with open('Fruit.txt', "r") as input:
    for line in input:
        if InputText in line:
           print(line, end='')
           print(next(input), end='')
           break

>> Pears
>> Lemons

Or with your original solution:

InputText = 'Apples'

infile = open('Fruit.txt',"r")
for line in infile:
    if InputText in line:
        print(line, end='')
        print(next(infile), end='')
        return
infile.close()

>> Apples
>> Oranges

Upvotes: 4

Mad Physicist
Mad Physicist

Reputation: 114548

There are a few things that you should consider changing in your code to make it work properly. Starting from the top:

  1. Consider using the file you open as a context manager in a with statement. To quote the explanation in the docs:

    It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks.

  2. The file object you are iterating over is an iterator (who'da thunk it?). That means that you can apply the builtin next function to it. This will work even if you do not break out of the loop because of the lazy iteration that file objects do. In general, unless you really know what you are doing, calling next on the iterator within a for loop may cause very unexpected results.

  3. Each line (except possibly the last) will contain a trailing \n character. print normally appends another newline to the strings that it prints. To avoid the double newline, you can add end='' to the arguments of print. end is a keyword-only argument that determines the line ending that print appends to its output.

  4. You need to clarify what you want to happen when the user enters the word on the last line of the file: 'Lemons' in your example. At the moment, using next will raise a StopIteration error. You can catch the error, transform it, or just add a blank line to the end of the file to make sure it does not happen.

  5. The use of return is illegal in a module-level loop. If your code is not in a function or method, you should use break to get out of the loop instead of return.

Here is what all that looks like in combination:

with open('Fruit.txt',"r") as file:
    for line in file:
        if InputText in line:
            print(line, end='')
            print(next(file), end='')
            break

Upvotes: 5

Related Questions