Johnny Apple Johnny Apple - 1 month ago 36
Python Question

Printing the next line in Pycharm with Python 3.6.2

I need to be able to print the next line in an opened text file provided the line prior starts with a certain string. The itertools method is not accomplishing what I'd like. Here is a sample of the text from which I'm printing:

Computer Name: PHCMIS034
Volume C: []
Key Protectors of Type Numerical Password
Numerical Password:
ID: {9A81DF1C-F9AA-4BB6-9089-A7D400E8F64A}
Password:
405042-041811-330022-078650-699875-584925-486079-464222


Here is the other topic: How to print next line in python

I want to print the line after any line that starts with "Password."
The itertools islice method does not seem to do much of anything, unless I'm mis-implementing it, nor does
print(next(op))
. (
op
is the name of my
open(filename)
).

Here is my full code:

def nonblank_lines(f):
nonempty_lines = []
for l in f:
line = l.rstrip()
if line:
yield line.lstrip()

with open("C:\\Users\\MyName\\Desktop\\BLRP.txt", "r+") as op:
for line in nonblank_lines(op):
if line.startswith("Computer Name") or line.startswith("ERROR") or line.startswith("ID"):
print(line)
elif line.startswith("Password"):
***print(next(op))***
#What do I do here?
op.close()

Answer Source
with open("C:\\Users\\MyName\\Desktop\\BLRP.txt", "r+") as op:
    instance_of_generator = nonblank_lines(op)
    for line in instance_of_generator:
        if line.startswith("Computer Name") or line.startswith("ERROR") or line.startswith("ID"):
            print(line)
        elif line.startswith("Password"):
            print(next(instance_of_generator))