Accountant who does not count

Asked

Viewed 72 times

1

Hello I did the following program:

#!/usr/bin/env python3
# -*- coding: utf8 -*-

import datetime
import os
import sys

ref_arquivo = open("trata.txt","r")

for linha in ref_arquivo:
    valor = linha.split(" ")
    recebido0=0
    send0=0
    recebido1=0
    send1=1
    if (valor[0] == "0"):
        if ( valor[1] == "RECEBIDO" ):
            recebido0 = recebido0 + 1
            print ("RECEBIDO 00000")
        elif ( valor[1] == "SENDING" ):
            send0 = send0 + 1
            print ("SEND 00000")

    else:
        if (valor[0] == "1"):
            if ( valor[1] == "RECEBIDO" ):
                recebido1 = recebido1 + 1
                print ("RECEBIDO 11111")
            elif ( valor[1] == "SENDING" ):
                send1 = send1 + 1
                print ("SEND 1111")

print ("=========================")
print ('Recebidos [0] =', recebido0)
print ('Enviados [0] =', send0)
print ("=========================")
print ('Recebidos [1] =', recebido1)
print ('Enviados [1] =', send1)

ref_arquivo.close()

This program takes the line of a txt file, removes the first character (0 or 1) and makes the condition (IF). Regardless of the return of the condition, it enters a second condition to verify whether it is SEND or RECEIVED. Following the prints, it’s working, but when I check the counters, it’s not incremented. At the level of clarification, there are 9 RECEIVED, 5 from zero and 4 from 1 and 33 SENDING, and after execution the counters have

=========================
Recebidos [0] = 0
Enviados [0] = 0
=========================
Recebidos [1] = 0
Enviados [1] = 1

Can someone help me?

  • 3

    Counters are being declared inside the loop, so they are reset every iteration, they have to be declared before the loop.

  • Failed to post the contents of the.txt file so the community can test your code.

2 answers

1

was taking a look at your code. You should perform 2 adjustments, the variables of counters should be out of the loop and should remove the \n of the value variable.

import datetime
import os
import sys

ref_arquivo = open("trata.txt","r")

recebido0=0
send0=0
recebido1=0
send1=1

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

    if (valor[0] == "0"):
        if ( valor[1] == "RECEBIDO" ):
            recebido0 = recebido0 + 1
            print ("RECEBIDO 00000")
        elif ( valor[1] == "SENDING" ):
            send0 = send0 + 1
            print ("SEND 00000")

    else:
        if (valor[0] == "1"):
            if ( valor[1] == "RECEBIDO" ):
                recebido1 = recebido1 + 1
                print ("RECEBIDO 11111")
            elif ( valor[1] == "SENDING" ):
                send1 = send1 + 1
                print ("SEND 1111")

print ("=========================")
print ('Recebidos [0] =', recebido0)
print ('Enviados [0] =', send0)
print ("=========================")
print ('Recebidos [1] =', recebido1)
print ('Enviados [1] =', send1)

ref_arquivo.close()

Variable result value of the old code:

inserir a descrição da imagem aqui

Result of updated code:

inserir a descrição da imagem aqui

  • Did not work using line value[1] = value[1]. replace(' n','')

  • The error was Traceback (Most recent call last): File "le.py", line 16, in <module> value[1] = value[1]. replace(' n',') Indexerror: list index out of range

  • Your dataset comes in this format? 0 RECEIVED 1 SEND 1 SEND ??

  • More or less. It comes, for example: 0 SENDING BEACON #0

  • That’s because I handled the file. I actually tried to split it into this format: 0.203537606789 SN.Node[1]. Application SENDING BEACON 0 but gave the same error

  • can paste two lines of your file here ? as I understand it does not break line

  • I will paste the original lines and then the ones I treated. If you can help me with the split to treat the original, it will be wonderful.

  • 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.231689895368 SN.Node[0].Communication.Radio TX finished (no more pkts in the buffer) Changing to RX 0.231689895368 SN.Node[1].Communication.Radio Received Packet (WC_SIGNAL_END) from Node 0, with no Interference 0.231699895368 SN.Node[1]. Application RECEIVED BEACON #0 - ORIGIN 0 - JUMPS 0

  • there is plenty of space between the initial double and SN.xxx as well as many spaces before SENDING or RECEIVED

  • then I processed the file and it was like this

  • 0 SENDING BEACON # 0 0 Sending Beacon 0 Buffered Bypassrouting Packet 0 SET STATE to TX, 0 completing Transition to 1 0 Sending Packet, Transmission will 1 START Signal from Node 0 TX finished (no more 1 END Signal from Node 1 Received Packet (WC_SIGNAL_END) from 0 SET STATE to RX, 0 completing Transition to 0 1 RECEIVED BEACON #0 -

  • 0 SENDING BEACON # 0

  • 0 Sending Beacon

  • 0 completing Transition to 1

  • 1 START Signal from Node

  • 1 Received Packet (WC_SIGNAL_END) from

  • 1 BEACON RECEIVED #0 -

Show 12 more comments

0

As commented, the counters are being initialized at each loop with zero value, as they are "participating" in the loop.

To solve, it is enough that they are initialized only once, being outside the loop.

Code with the same corrected example:

import datetime
import os
import sys

ref_arquivo = open("trata.txt","r")

valor = linha.split(" ")
recebido0=0
recebido1=0
send1=1

for linha in ref_arquivo:
    if (valor[0] == "0"):
        if ( valor[1] == "RECEBIDO" ):
            recebido0 = recebido0 + 1
            print ("RECEBIDO 00000")
        elif ( valor[1] == "SENDING" ):
            send0 = send0 + 1
            print ("SEND 00000")

    else:
        if (valor[0] == "1"):
            if ( valor[1] == "RECEBIDO" ):
                recebido1 = recebido1 + 1
                print ("RECEBIDO 11111")
            elif ( valor[1] == "SENDING" ):
                send1 = send1 + 1
                print ("SEND 1111")

print ("=========================")
print ('Recebidos [0] =', recebido0)
print ('Enviados [0] =', send0)
print ("=========================")
print ('Recebidos [1] =', recebido1)
print ('Enviados [1] =', send1)

ref_arquivo.close()
  • It worked perfect. Thank you!!!

Browser other questions tagged

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