在Python中从.txt文件读取数据的特定部分

时间:2019-02-01 15:02:42

标签: python python-3.x

>gene1
ATGATGATGGCG
>gene2
GGCATATC
CGGATACC
>gene3
TAGCTAGCCCGC

这是我尝试读取的文本文件。 我想读取每个基因的不同字符串,然后将其添加到列表中 标题行以“>”字符开头,以识别这是基因的开始还是结尾

with open('sequences1.txt') as input_data:
    for line in input_data:
            while line != ">":
                list.append(line)
    print(list)

打印时,列表应显示的列表应为

list =["ATGATGATGGCG","GGCATATCCGGATACC","TAGCTAGCCCGC"]

4 个答案:

答案 0 :(得分:2)

with open('sequences1.txt') as input_data:
    sequences = []
    gene = []
    for line in input_data:
        if line.startswith('>gene'):
            if gene:
                sequences.append(''.join(gene))
                gene = []
        else:
            gene.append(line.strip())
sequences.append(''.join(gene)) # append last gene
print(sequences)

输出:

['ATGATGATGGCG', 'GGCATATCCGGATACC', 'TAGCTAGCCCGC']

答案 1 :(得分:0)

sequences1.txt:

>gene1
ATGATGATGGCG
>gene2
GGCATATC
CGGATACC
>gene3
TAGCTAGCCCGC

然后:

desired_text = []
with open('sequences1.txt') as input_data:
    content = input_data.readlines()
    content = [l.strip() for l in content if l.strip()]
    for line in content:
            if not line.startswith('>'):
                desired_text.append(line)

print(desired_text)

输出:

['ATGATGATGGCG', 'GGCATATC', 'CGGATACC', 'TAGCTAGCCCGC']

编辑:

快速阅读,并用所需的输出进行固定

with open('sequences1.txt') as input_data:
    content = input_data.readlines()
    # you may also want to remove empty lines
    content = [l.strip() for l in content if l.strip()]
    # flag
    nextLine = False
    # list to save the lines
    textList = []
    concatenated = ''
    for line in content:
        find_TC = line.find('gene')

        if find_TC > 0:
            nextLine = not nextLine
        else:
            if nextLine:
                textList.append(line)
            else:
                if find_TC < 0:
                    if concatenated != '':
                        concatenated = concatenated + line
                        textList.append(concatenated)
                    else:
                        concatenated = line

print(textList)

输出:

['ATGATGATGGCG', 'GGCATATCCGGATACC', 'TAGCTAGCCCGC']

答案 2 :(得分:0)

您的代码中有多个错误,请看这里:

with open('sequences1.txt', 'r') as file:
    list = []
    for line in file.read().split('\n'):
            if not line.startswith(">") and len(line$
                list.append(line)
    print(list)

答案 3 :(得分:0)

尝试一下:

$ cat genes.txt
>gene1
ATGATGATGGCG
>gene2
GGCATATC
CGGATACC
>gene3
TAGCTAGCCCGC


$ python
>>> genes = []
>>> with open('genes.txt') as file_:
...   for line in f:
...     if not line.startswith('>'):
...       genes.append(line.strip())
...
>>> print(genes)
['ATGATGATGGCG', 'GGCATATC', 'CGGATACC', 'TAGCTAGCCCGC']