gravatar for projetoic

3 hours ago by

When counting the number of characters at the beginning of a string or the end of a string, the result is 0. What do I do?

f = open('Denv4-X-gb_AY947539.txt', 'r') 
z = f.read()
e = sum(1 for _ in it.takewhile(lambda c: c == '-', z)) 
y = sum(1 for_ in it.takewhile(lambda c: c == '-', reversed(z)))
print(e,y)

Output:

> 0

The file is being read normally ....

What's in the file:

>lcl|NC_002640.1_cds_NP_073286.1_1 [gene=POLY] [locus_tag=DV4_gp1] [db_xref=GeneID:5075729] [protein=polyprotein] [protein_id=NP_073286.1] [location=102..10265] [gbkey=CDS]
------------------------------------------------------------
---------------------------------atgaaccaacgaaaaaaggtggttaga             
ccacctttcaatatgctgaaacgcgagagaaaccgcgtatcaacccctcaagggttggtg
aagagattctcaaccggacttttttctgggaaaggacccttacggatggtgctagcattc
atcacgtttttgcgagtcctttccatcccaccaacagcagggattctgaagagatgggga
cagttgaagaaaaataaggccatcaagatactgattggattcaggaaggagataggccgc
------------------------------------------------------------
>gb:AY947539|Organism:Dengue virus 4|Strain Name:H241|Segment:null|Subtype:4|Host:Human
ggtcgtgtggaccgacaaggacagttccaaatcggaagcttgcttaacacagttctaaca
gtttgtttagatagagagcagatctctggaaaaatgaaccaacgaaaaaaggtggttaga
ccacctttcaatatgctgaaacgcgagagaaaccgcgtatcaacccctcaagggttggtg
aagagattctcaaccggacttttttccgggaaaggacccttacggatggtgctagcattc
atcacgtttttgcgagtcctttccatcccaccaacagcagggattctgaaaagatgggga
cagttgaagaaaaacaaggccatcaaaatactgactggattcaggaaggagataggccgc
atgctgaacatcttgaatggaagaaaaaggtcaacaatgacattgctgtgcttgattccc

I would like to know how to print the "-" number at the beginning and end of the first text:

before starting

>lcl|NC_002640.1_cds_NP_073286.1_1 [gene=POLY] [locus_tag=DV4_gp1] [db_xref=GeneID:5075729][protein=polyprotein] [protein_id=NP_073286.1] [location=102..10265] [gbkey=CDS]

Blockquote

>gb:AY947539|Organism:Dengue virus 4|Strain Name:H241|Segment:null|Subtype:4|Host:Human.

.

f = open('Denv4-X-gb_AY947539.txt.txt', 'r')
con = f.readlines()
con = [i.strip() for i in con]
length = len(con[0].split(" ")[1])
result = f'{con[1].split(" ")[0]} {con[1].split(" ")[1][length:]}'
print(result)
f.close()

f = open('file.txt', 'a')
f.write(f'n{result}')



Source link