Q: parsing a file in python with for loop I am trying to parse a file with a limited knowledge in python with some loops but seems I'm doing something wrong. I want to get the genes(sp...) if values in lst are between the numbers in the middle. Here's the file (ast.txt):
SPAC212.11  1   5662    -1

SPAC212.10  5726    6331    -1

SPAC212.09c 7619    9274    1

SPNCRNA.70  11027   11556   -1

SPAC212.08c 11784   12994   1

SPAC212.07c 13665   14555   1

SPAC212.12  15855   16226   1

SPAC212.06c 18042   18306   1

and here's my code:
lst=[2,6000,18042,11784]

f=open('asd.txt','r')

g=f.readlines()[0:]

for line in g:
    for s in lst:
        if (s)>=int(line.split()[1:2]) and (s)<=int(line.split()[2:3]):
            line.split()[0:1]


A: A few suggestions:
output = [] # lines to keep; empty for now

with open("asd.txt", 'r') as f: # use with to handle file open/close

    for line in f: # iterate through lines

        line = line.strip().split() # split the line once

        if any(int(line[1]) <= n <= int(line[2]) for n in lst): # check

            output.append(line[:]) # add copy to output

# use lines in output

Note that this will keep the line in split() form as a list of str values, e.g.:
output == [['SPAC212.07c', '13665', '14555', '1'], ...]

