Split returning Indexerror

Asked

Viewed 66 times

-1

I have a file that the lines have this pattern:

0.22755189537     SN.node[0].Application                   SENDING BEACON # 0
0.22755189537     SN.node[0].Application                   Sending [Beacon] of size 105 bytes to communication layer
0.22755189537     SN.node[0].Communication.Radio           Buffered [BypassRouting packet] from MAC layer
0.22755189537     SN.node[0].Communication.Radio           SET STATE to TX, delay=1e-05, power=62
0.227561895369    SN.node[1].Communication.Radio           START signal from node 0 , received power -87.6124dBm
0.231689895368    SN.node[0].Communication.Radio           TX finished (no more pkts in the buffer) changing to RX
0.231689895368    SN.node[1].Communication.Radio           END signal from node 0
0.231689895368    SN.node[1].Communication.Radio           Received packet (WC_SIGNAL_END) from node 0, with no interference
0.231699895368    SN.node[1].Application                   RECEBIDO BEACON #0 - ORIGEM 0 - SALTOS 0

I tried to split to get the value between [ ], like this: x.split()[1]. split('[')[1][0] but gives Indexerror error: list index out of range.

1 answer

0


Assuming you only want to separate the value between the brackets.

Read each line of arquivo.txt and slicing into the desired column.

Then just add the cut extracted from the line into a list.

lista=[] 

arq = open("arquivo.txt", "r")

for linha in arq :

        lista.append(linha[26:27])

arq.close()

To print the list:

for l in lista:

        print(l)

Exit:

0
0
0
0
1
0
1
1
1
  • It worked correctly, but I was trying the split, because I need other information from the line as well. To clarify: I take the value of a node (0 or 1) and then in the third part I have an occurrence. I want to check how many occurrences have arisen of the RECEIVED type for each node (only one counter) and how many SENDING occurrences have also occurred for each node.

  • What it would look like to get out of what you need?

  • Right now it’s pretty simple, something like: Node[0] - x SENT

  • Node[1] - x RECEIVED

  • In fact there are in all 36 nodes (0 to 35) and I would have to read the file, and check each of the nodes how many RECEIVED and how many SENDING occurred.

  • Only I do not know where to start with 36 knots, with two I thought of if/Else (if it is 1...if not 0, kkk)

  • With 36 I thought of creating a case with 36 entries, each row corresponds to a node, so the corresponding row in the case would have a received if (received ++) Elif sending (sendingx ++) Else nothing. But I don’t know if my logic is right.

  • finally the output could be equal to a table

  • Node | SENT | RECEIVED

  • 1 | xxxx | xxxx

  • I don’t know if you understood how it works to slice the line... For example: linha[26:27] returns only 0 or 1 because it begins to slice on column 26 and ends on 27. If it were linha[25:28] the exit would be [0] or [1] and so on. Modify in the example of my answer to better understand. Try the logic you imagined and if it doesn’t work ask a new question. Good luck!

  • Eden, after I followed this logic and the idea you gave me, I realized it didn’t work

  • note that there are more than 100,000 lines enter the append only works in the first part

  • Post your code and error message (if you have).

  • Éder, I’ve changed a lot of things and managed to improve a little, using the split. But it’s still a long way to go. If you want to take a look https://repl.it/repls/GrizzledVillainousDebuggers

  • I looked at the code. The logic is wrong. I need to understand what you want to do. So let’s see if I understand you: If SENDING or Sending is equal to 1 then contador_send is incremented. Then, if Received or RECEBIDO is equal to 1 then contador_recebido is incremented. This is what you need?

  • Hello Eden, it would not be that. I managed to do after catching a little. Look, I have two nodes that send x packages between them. I need to count how many packets each node sent and how many each node received. Then I did an if to check if the node is 0, if not, it can only be 1. Inside the if/Else I did a second if checking RECEIVED, but SENDING. That’s the logic I followed.

  • I am glad that it worked. With patience and persistence to overcome a lot of things. I am glad that you succeeded in your endeavor. Congratulations!

Show 13 more comments

Browser other questions tagged

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