简体   繁体   中英

Reading a specific portion of data from a .txt file in Python

>gene1
ATGATGATGGCG
>gene2
GGCATATC
CGGATACC
>gene3
TAGCTAGCCCGC

This is the text file which I am trying to read. I want to read every gene in a different string and then add it in a list There are header lines starting with '>' character to recognize if this is a start or end of a gene

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

When printed the list should display list should be

list =["ATGATGATGGCG","GGCATATCCGGATACC","TAGCTAGCCCGC"]
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)

output:

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

sequences1.txt:

>gene1
ATGATGATGGCG
>gene2
GGCATATC
CGGATACC
>gene3
TAGCTAGCCCGC

and then:

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)

OUTPUT:

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

EDIT:

Sped-read it, fixed it with the desired output

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)

OUTPUT:

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

You have multiple mistakes in your code, look here:

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)

Try this:

$ 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM