使用for循环解析python中的文件

使用for循环解析python中的文件,python,Python,我试图用一些循环解析一个python知识有限的文件,但似乎我做错了什么。我想得到基因(sp…),如果在 LST < /代码>中的值在中间的数字之间。这是文件(ast.txt): 这是我的代码: 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)&

我试图用一些循环解析一个python知识有限的文件,但似乎我做错了什么。我想得到<代码>基因(sp…)<代码>,如果在<代码> LST < /代码>中的值在中间的数字之间。这是文件(
ast.txt
):

这是我的代码:

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]
lst=[260001804211784]
f=打开('asd.txt','r')
g=f.readlines()[0:]
对于g行:
对于lst中的s:
如果(s)>=int(line.split()[1:2])和(s)一些建议:

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

请使用编辑器中的“代码标记”功能,将代码与文件数据分开。我不知道该功能,但我正在尝试了解。谢谢。我尝试了您的代码,但输出列表为空,我不知道为什么。但我对您的代码做了一点更改,效果很好。对于lst中的n:if int(第[1]行)表示歉意,编辑以复制整个
行。请注意,
line[0:1]==line[0]
。无需道歉,伙计,非常感谢您抽出时间。
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
output == [['SPAC212.07c', '13665', '14555', '1'], ...]