1

At the moment I have a block of code that allows me to find an exact line in a notepad file and the add it to GTINlist. However I also want to add the line below that, and the line below that as well. I do not however want to import the rest of the file as a list. This is my code at the moment:

GTINlist=[]
GTIN=input("Please enter your GTIN code. ")
GTINcodes = [line for line in open('GTINcodes.txt') if GTIN in line]
stringGTINcode = str(GTINcodes)
GTINlist.append(stringGTINcode)*

3 Answers 3

0

Here is what I did:

GTIN=input("Please enter your GTIN code. ")
with open('GTINcodes.txt', 'r') as file:
    GTINcodes = file.readlines()  #this seperates the lines of the file into the list called GTINcodes
GTINlist = GTINcodes[GTINcodes.index(GTIN):GTINcodes.index(GTIN) + 3]  #create the list GTINlist starting from the index where GTIN is found and add next two lines
2
  • Ah yes, this is also a good solution if GTINcodes.txt is not too big and if you are interrested only on the first occurence of GTIN.You can store GTINcodes.index(GTIN) in a variable to prevent double computation. Feb 29, 2016 at 12:58
  • Maybe OP edited the question, but it now says "I do not however want to import the rest of the file as a list."
    – gil
    Feb 29, 2016 at 15:42
0

You cannot use list comprehension in that case. But you can do this:

GTINlist=[]
GTIN=input("Please enter your GTIN code. ")
GTINcodes = []
read_ahead = 0
for line in open('GTINcodes.txt'):
    if GTIN in line:
        GTINcodes.append(line)
        read_ahead = 2
    elif read_ahead > 0:
        GTINcodes.append(line)
        read_ahead -= 1
stringGTINcode = str(GTINcodes)
GTINlist.append(stringGTINcode)*
1
  • Thank you this is incredibly useful. Feb 29, 2016 at 12:59
0

The builtin next() advances an iterator by one step. So in your case:

# setup
GTIN = input("Please enter your GTIN code. ")
GTINcodes = []


extra = 2  # number of extra lines to be appended

with open('GTINcodes.txt') as f:
    for line in f:
        if GTIN in line:
            GTINcodes.append(line)
            for _ in range(extra):
                GTINcodes.append(next(f))
            # if need to loop through the rest of the file, comment out break
            break

This can be further simplified by using itertools.dropwhile to easily skip lines without GTIN in them. dropwhile takes a predicate and an iterable, and returns an iterator which yields values from the iterable starting from the first value for which the predicate is false. So:

from itertools import dropwhile

# setup
GTIN = input("Please enter your GTIN code. ")

lines_to_take = 3  # first line with GTIN in it, and 2 lines after it
lines = dropwhile(lambda line: GTIN not in line, open('GTINcodes.txt'))
GTINcodes = [next(lines) for _ in range(lines_to_take)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.