Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何每次跳过文件中的一行?_Python_Python 3.x - Fatal编程技术网

在python中,如何每次跳过文件中的一行?

在python中,如何每次跳过文件中的一行?,python,python-3.x,Python,Python 3.x,文件格式: >ackg_2341 ACGATACGACGACATCA >ackg_7865 GCACTACGCAGAAACGAA >... 每次我都想跳过带有“>”的那行。 我建议这样做,但它不起作用 f = open("data.txt","r") lcs = '' if f.read(1)=='>': str1 = f.readline[1:] for line in f: if line.read(1)=='

文件格式:

>ackg_2341
ACGATACGACGACATCA
>ackg_7865
GCACTACGCAGAAACGAA
>...
每次我都想跳过带有“>”的那行。 我建议这样做,但它不起作用

f = open("data.txt","r")
    lcs = ''
    if f.read(1)=='>':
        str1 = f.readline[1:]
    for line in f:
        if line.read(1)=='>'
        temp = ''.join(f.readline[1:])
        res = len(lcs_matrix(str1,temp))

        if len(lcs)<res:
            lcs = lcs_matrix(str1, temp)

print(lcs)
f=open(“data.txt”、“r”)
lcs=“”
如果f.read(1)='>':
str1=f.readline[1:]
对于f中的行:
如果line.read(1)='>'
temp=''.join(f.readline[1:])
res=len(lcs_矩阵(str1,温度))
如果len(lcs)只需这样做

f = open("data.txt", "r")
n=1#lines you want to skip
for line in f.readlines()[n:]:
    if  line.startswith('>'):
        "what ever you want"
    else:
        print(line)
就这么做吧

f = open("data.txt", "r")
n=1#lines you want to skip
for line in f.readlines()[n:]:
    if  line.startswith('>'):
        "what ever you want"
    else:
        print(line)

该文件格式通常称为FASTA,在分子生物学中用于存储基因/蛋白质序列,其中以
开头的行是每个序列的“头”,其他行是序列

对于此类文件,可能还需要合并可能跨越多行的序列数据。下面是一个函数,用于读取文件、拆分头和序列、合并序列(如果它们跨越多行),然后返回文件中所有头的列表和所有序列的列表。因此,您可以根据自己的喜好循环浏览标题和序列列表

def readFasta(fasta_文件):
打开(fasta_文件,'r')的速度与打开的速度一样快:
标题,序列=[],[]
对于线路输入fast:
如果line.startswith('>'):
head=行。替换('>','').strip()
headers.append(head)
sequences.append(“”)
其他:
seq=行.带()
如果len(seq)>0:
序列[-1]+=seq
返回[标题、序列]
示例:fasta.txt中的数据

>header1
ACGATACGACGACATCA
>header2
GCACTACGC
AGAAACGAA
>header3
ACGATCGA
ACGATTAC
[headers,seqdata]=readFasta(fasta.txt)
对于范围内的i(len(headers)):
打印(标题[i])
打印(seqdata[i])
打印()
输出:

header1
ACGATACGACATCA
校长2
GCACTACGCAGAACGAA
校长3
ACGATCGACGAACGATTAC

该文件格式通常称为FASTA,在分子生物学中用于存储基因/蛋白质序列,其中以
开头的行是每个序列的“标题”,其他行是序列

对于此类文件,可能还需要合并可能跨越多行的序列数据。下面是一个函数,用于读取文件、拆分头和序列、合并序列(如果它们跨越多行),然后返回文件中所有头的列表和所有序列的列表。因此,您可以根据自己的喜好循环浏览标题和序列列表

def readFasta(fasta_文件):
打开(fasta_文件,'r')的速度与打开的速度一样快:
标题,序列=[],[]
对于线路输入fast:
如果line.startswith('>'):
head=行。替换('>','').strip()
headers.append(head)
sequences.append(“”)
其他:
seq=行.带()
如果len(seq)>0:
序列[-1]+=seq
返回[标题、序列]
示例:fasta.txt中的数据

>header1
ACGATACGACGACATCA
>header2
GCACTACGC
AGAAACGAA
>header3
ACGATCGA
ACGATTAC
[headers,seqdata]=readFasta(fasta.txt)
对于范围内的i(len(headers)):
打印(标题[i])
打印(seqdata[i])
打印()
输出:

header1
ACGATACGACATCA
校长2
GCACTACGCAGAACGAA
校长3
ACGATCGACGAACGATTAC

f.readline
是一种方法,如错误消息所示。方法和函数是不可下标的。您打算用open(“file.txt”)调用类似于
f.readline()
的方法作为f:
<代码>用于f中的行:
<代码>如果行.startswith(“>”):继续<代码>其他:。。。看起来做你想做的事更合乎逻辑do@PatrickArtner我希望将第一个字符串分开,并与文件中的其余字符串进行比较。您知道如何设置读取文件的范围吗?例如,For循环应该从第3行开始,并遍历文件中的其余行。这是否回答了您的问题
f.readline()[2://code>将把整个文件读入内存,
next(f)
两次和
for line in f:
after将保持内存较低,因为在跳过前两行后,您只将一行读入内存。
f.readline
是一种方法,如错误消息所述。方法和函数是不可下标的。您打算用open(“file.txt”)调用类似于
f.readline()
的方法作为f:
<代码>用于f中的行:
<代码>如果行.startswith(“>”):继续<代码>其他:。。。看起来做你想做的事更合乎逻辑do@PatrickArtner我希望将第一个字符串分开,并与文件中的其余字符串进行比较。您知道如何设置读取文件的范围吗?例如,For循环应该从第3行开始,并遍历文件中的其余行。这是否回答了您的问题
f.readline()[2:://code>将把整个文件读入内存,
next(f)
两次和
对于f:
after中的行,将保持内存较低,因为在跳过前两行后,只将1行读入内存。