-1
Guys, I need your help. I have a code in python that I would like to return the amount of values that meet a criterion, but it is returning zero. The txt file contains data like this:
567886786765211267878783647866575678666869837678681226786789 cancelado
562342342342343123124234534645656756756756756756756765756178 assinado
555456456546544324234235434534645645645645654645645645645631 assinado
520757345873458374657834857897989988786785645342111575756432 cancelado
554234534534645647567867987089078655345231342536456575676777 cancelado
That’s the return I’m getting:
{'P1': {'Total': 2, 'Ativos': 0}, 'P2': {'Total': 2, 'Ativos': 0}, 'P3': {'Total': 1, 'Ativos': 0}}
And I’d like him to return to me like this:
{'P1': {'Total': 2, 'Ativos': 0}, 'P2': {'Total': 2, 'Ativos': 1}, 'P3': {'Total': 1, 'Ativos': 1}}
That is the code
with open('log.txt','r') as file:
arquivo = filter(None, [line.rstrip('\n').split() for line in file])
prefixo = []
status = []
for linha in arquivo:
prefixo.append(linha[0][:2])
status.append(linha[1])
data = zip(prefixo, status)
paises = {
'P1': {
'Total': [p for p in prefixo if p == '55'].count('55'),
'Ativos': [d for d in data if d[0] == '55' and d[1] == 'assinado'].count('assinado')
},
'P2': {
'Total': [p for p in prefixo if p == '56'].count('56'),
'Ativos': [d for d in data if d[0] == '56' and d[1] == 'assinado'].count('assinado')
},
'P3': {
'Total': [p for p in prefixo if p == '52'].count('52'),
'Ativos': [d for d in data if d[0] == '52' and d[1] == 'assinado'].count('assinado')
},
}
If you can help me
Without seeing the file (I’m assuming you have, because of the
for linha in arquivo
), we can only guess: put[ d[1] for d in data etc...
(instead of justd for d in data
). Anyway, could [Edit] and put some lines of the file as example?– hkotsubo
@hkotsubo I was trying to edit the post and was not getting it. I will edit but I cannot share the file.
– esabin
@hkotsubo I made the change you mentioned, but continues with the same result
– esabin
It doesn’t have to be the whole file, it could just be a piece that reproduces the problem, otherwise we can’t even test, let alone know what’s wrong... Or, change the data to something that can be disclosed, as long as the main problem remains (count to zero). Otherwise we will stay in this divination ("try it", "did not give"), and as you realized, it is not productive
– hkotsubo
hkotsubo take a look and see if you can understand
– esabin