3
I copied the following code from a data visualization tutorial in Python, but it does not perform properly, warning Keyerror as seen in the title. Can someone help me? There’s something wrong there?
entrada = open("/bacteria.fasta").read()
saida = open("/bacteria.html", "w")
cont = {}
for i in ['A', 'T', 'C', 'G']:
for j in ['A', 'T', 'C', 'G']:
cont[i+j] = 0
entrada = entrada.replace("\n", "")
for k in range(len(entrada)-1):
cont[entrada[k]+entrada[k+1]] += 1
# html
i = 1
for k in cont:
transparencia = cont[k]/max(cont.values())
saida.write("<div style='width:100px; border:1px solid #111; height:100px; float:left; background-color:rgba(0,0,255,"+str(transparencia)+"')></div>")
saida.close()
The log says the following:
KeyError Traceback (most recent call last)
<ipython-input-9-e27a0dec51b4> in <module>()
13
14 for k in range(len(entrada)-1):
---> 15 cont[entrada[k]+entrada[k+1]] += 1
16
17 # html
KeyError: '>R'
Can you please put the full bug log in the question?
– Augusto Vasques
Okay, I’ll log
– lina dias
Here in
entrada[k+1]
the indexk+1
in the last elementk
violates the list sizeentrada
.– Augusto Vasques
Open in block of text and delete the information not referring to the genetic code. For me, it worked right after
– Mateus Aquila Barroso