Copy line and write the same line to another file using python

Asked

Viewed 205 times

0

I need to open a file, read a particular line and write that same line to another file. However, at the end of the line need to add a character (an asterisk "*").

leitura = open(arqfasta, 'r')
linha2 = leitura.readlines()[1]
leitura.close()
print(linha2)
#linha2 quanda como valor a segunda linha do arquivo fasta que contém a sequencia de aa.

arquivo = open(arqali, 'w')
arquivo.write('>P1;pep' + str(i) + '\n')
arquivo.write('sequence:pep' + str(i) + ':::::::0.00: 0.00 \n')
arquivo.write(linha2 + '*')
arquivo.close()

When executing the code, I get this output in the file (as it is in the image):

IIISVLKGSYIFTSDFIRYLGDCGLPHVVDFVRLASYNSGTK
*

enter image description here

Since you would like to get this asterisk at the end of the string:

IIISVLKGSYIFTSDFIRYLGDCGLPHVVDFVRLASYNSGTK*

I can’t understand why he puts the final character (the asterisk) on the next line, and the correct one would be to put at the end of the string.

2 answers

0

Use rstrip() to remove all spaces, new lines and pipes.

Whenever you want to "see" if there is any type of white space in a string try to transfer it into a list:

palavra = 'teste \n\t\r'
print(palavra)
print([palavra])
print([palavra.rstrip()])

Try:

leitura = open(arqfasta, 'r')
#  usando o rstrip()
linha2 = leitura.readlines()[1].rstrip()
leitura.close()
print(linha2)
#linha2 quanda como valor a segunda linha do arquivo fasta que contém a sequencia de aa.

arquivo = open(arqali, 'w')
arquivo.write('>P1;pep' + str(i) + '\n')
arquivo.write('sequence:pep' + str(i) + ':::::::0.00: 0.00 \n')
arquivo.write(linha2 + '*')
arquivo.close()

0


First of all, readlines reads all the lines of the file, returning them in a list. But since you only want the second line, you don’t have to read them all:

with open(arqfasta) as leitura, open(arqali, 'w') as saida:
    next(leitura) # ignora a primeira linha
    linha2 = next(leitura).rstrip('\r\n') # lê a segunda linha

Like an archive is eternal, when calling next i get the next "item" from an iterable (which in the case of the file, will be the next line). That is, the first time I get the first line (and as I am not keeping this value in any variable, the same is ignored) and the second time I get the second line, which is what you want.

I used rstrip to remove the line break from the end (since that’s why your output typed the asterisk on another line, because line breaks are also returned from reading). And I also used with to open the file, so it will already be closed automatically.

Finally, you can write to another file using the same with:

with open(arqfasta) as leitura, open(arqali, 'w') as saida:
    next(leitura)
    linha2 = next(leitura).rstrip('\r\n')
    saida.write(f'>P1;pep{i}\n')
    saida.write(f'sequence:pep{i}:::::::0.00: 0.00 \n')
    saida.write(f'{linha2}*')

I used f-string to format the output (available from Python 3.6), so you avoid concatenating strings. If you are using a version earlier than 3.6, you can use format:

with open(arqfasta) as leitura, open(arqali, 'w') as saida:
    next(leitura)
    linha2 = next(leitura).rstrip('\r\n')
    saida.write('>P1;pep{}\n'.format(i))
    saida.write('sequence:pep{}:::::::0.00: 0.00 \n'.format(i))
    saida.write('{}*'.format(linha2))

If you want any line (not necessarily the second one), you can generalize the solution using the itertools.islice:

from itertools import islice

n_linha = 5 # obter a quinta linha
with open(arqfasta) as leitura, open(arqali, 'w') as saida:
    try:
        linha2 = next(islice(leitura, n_linha - 1, n_linha)).rstrip('\r\n')

        saida.write('>P1;pep{}\n'.format(i))
        saida.write('sequence:pep{}:::::::0.00: 0.00 \n'.format(i))
        saida.write('{}*'.format(linha2))
    except StopIteration:
        print(f'arquivo não tem {n_linha} linhas')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.