str.replace with Indexerror: list index out of range

Asked

Viewed 63 times

-2

I have the following code that returns

ref_arquivo = open("Castalia-tratado.txt","r")

for linha in ref_arquivo:
    valor = linha.split(" ")
    valor[2] = valor[2].replace('\n','')
    print (valor)

if you comment on the line valor[2]=valor[2],replace('\n','') the print prints:

['0', 'END', 'signal\n']
['0', 'Received', 'packet\n']
['1', 'SET', 'STATE\n']
['1', 'completing', 'transition\n']
['0', 'RX', '>>\n']
['0', 'TRATAR', 'DADOS\n']
['0', 'XXXXXXXXXXXX', 'SINK\n']
['0', 'SINK', 'Received\n']

With the uncommented line gives the following error:

Traceback (most recent call last):
File "le.py", line 16, in <module>
valor[2] = valor[2].replace('\n','')
IndexError: list index out of range

I’m trying to make a if to check whether the field valo[1] is equal to a given text AND valor[2] is the same as another text, in this way:

if ( valor[1] == "RECEBIDO" ) and ( valor[2] == "BEACON" ):
            recebido1 = recebido1 +1
elif ( valor[1] == "SENDING" ) and ( valor[2] == "BEACON" ):
            send1 = send1 + 1

Which also gives the same index error.

  • 3

    Its code worked as expected, without giving the error that it claims to have given: https://repl.it/@acwoss/sopt-Question-382655. Please edit your question and work out a [mcve] demonstrating the problem. Also make sure you’re analyzing the right code and on the right lines.

  • The error I reported is only the part that the terminal returns. The code itself is much bigger and anyway, it’s not working. the data file has more than 100,000 lines and I need the information 266150.195401607991SN.Node[1].Communication.Radio Sending Packet, Transmission will last 0.004128 secs (this is line 345.681) in square brackets (in case it is the node number) and occurrences, in this case Sending Packete (only these two strings)

  • What I want to do is check how many times RECEIVED BEACON and SENDING BEACON occurred in a few thousand line.

  • 3

    So it seems that the mistake is elsewhere, which you did not put in the question and thus there is no help. Maybe it’s some line in your 100,000+ line file that doesn’t have three values. It is up to you to check if it is the file that is wrong or if really the line may not have 3 values. We will only be able to help you from the moment you develop the [mcve] that really demonstrates your situation.

  • Hey, Anderson, thanks for your help. I put in https://repl.it/@acwoss/sopt-Question-382655 my current code, after several changes and an example of the actual log, because the one I passed was after treating it (to avoid errors like the one you mentioned, I preferred to use the original). I even managed to run the program, because it was a value passed wrong by me in the index within the second if that caused the error, but anyway it is not doing what it should do. Could you assist me?

  • I managed to fix it. Thanks for your help.

Show 1 more comment

1 answer

0

You can use the linha.strip("\n") to remove line break. If you need split the syntax is linha.split() to remove the space.

for linha in ref_arquivo:
    valor = linha.strip("\n") # remove a quebra de linha         
    print (valor)

Browser other questions tagged

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