0

Estou com o seguinte problema ao percorrer um arquivo de texto e não estou conseguindo resolver, acredito que seja simples a solução, mais preciso de ajuda para chegar a ela. Tenho o seguinte código:

import re



dic = {}

line = "rspamd_task_write_log: id: <CAP7Ane7t3GqwhbdnkcRiRD4vTR8wRVt=6yWTe7XYt6UC9yzjAQ@mail.test.com>, qid: <48H2js00X4zRj01>, ip: 209.85.208.65, from: <[email protected]>,\
       (default: F (no action): [-2.43/15.00] [IP_SCORE(-2.93){ip: (-2.32), ipnet: 123.123.123.0/17\
        (-4.14), asn: 15169(-3.28), country: US(-0.04);},SUSPICIOUS_RECIPS(1.50){},DMARC_POLICY_ALLOW(-0.50){test.com;none;},R_DKIM_ALLOW(-0.20){test.com:s=20161025;},R_SPF_ALLOW(\
        -0.20){+ip4:123.123.123.0/17;},MIME_GOOD(-0.10){multipart/mixed;multipart/alternative;text/plain;},ARC_NA(0.00){},ASN(0.00){asn:15169, ipnet:123.123.123.0/17, country:US;},DKI\
        M_TRACE(0.00){test.com:+;},FROM_EQ_ENVFROM(0.00){},FROM_HAS_DN(0.00){},HAS_ATTACHMENT(0.00){},MIME_TRACE(0.00){0:+;1:+;2:+;3:~;4:~;},PREVIOUSLY_DELIVERED(0.00){test@\
        test.com;},RCPT_COUNT_GT_50(0.00){174;},RCVD_COUNT_TWO(0.00){2;},RCVD_TLS_ALL(0.00){},TAGGED_RCPT(0.00){},TO_DN_SOME(0.00){},TO_MATCH_ENVRCPT_SOME(0.00){}]), len: 65925\
        08, time: 379.997ms real, 201.985ms virtual, dns req: 23, digest: <3ea7e074fbea648462253b1522858d71>, rcpts: <[email protected]>, mime_rcpts: <test@test\
        l.com,[email protected],[email protected],...>"


regexp = '(\]\ \[).*(\]\))'
pontuacao = '(\[-?\d*\.\d*\/)'
status = '(\:\s.\s\(.*\)\:\s)'
_id = '(\w{3}\:\s<\w*>,\s\w{2}:)'

score = re.findall(pontuacao, line)[0].strip('[').strip('/')
action = re.findall(status, line)[0].split('(')[1].strip('): ')
qid = re.findall(_id, line)[0].split('<')[1].split('>')[0]
signature = re.search(regexp, line).group().split(',')


dic[qid] = {'score': score, 'action': action, 'metrics': signature}

print(dic)

Que esta me retornando a seguinte saida que é exatamente o que preciso

{'48H2js00X4zRj01': {'score': '-2.43', 'action': 'no action', 'metrics': ['] [IP_SCORE(-2.93){ip: (-2.32)', ' ipnet: 123.123.123.0/17        (-4.14)', ' asn: 15169(-3.28)', ' country: US(-0.04);}', 'SUSPICIOUS_RECIPS(1.50){}', 'DMARC_POLICY_ALLOW(-0.50){test.com;none;}', 'R_DKIM_ALLOW(-0.20){test.com:s=20161025;}', 'R_SPF_ALLOW(        -0.20){+ip4:123.123.123.0/17;}', 'MIME_GOOD(-0.10){multipart/mixed;multipart/alternative;text/plain;}', 'ARC_NA(0.00){}', 'ASN(0.00){asn:15169', ' ipnet:123.123.123.0/17', ' country:US;}', 'DKI        M_TRACE(0.00){test.com:+;}', 'FROM_EQ_ENVFROM(0.00){}', 'FROM_HAS_DN(0.00){}', 'HAS_ATTACHMENT(0.00){}', 'MIME_TRACE(0.00){0:+;1:+;2:+;3:~;4:~;}', 'PREVIOUSLY_DELIVERED(0.00){test@        test.com;}', 'RCPT_COUNT_GT_50(0.00){174;}', 'RCVD_COUNT_TWO(0.00){2;}', 'RCVD_TLS_ALL(0.00){}', 'TAGGED_RCPT(0.00){}', 'TO_DN_SOME(0.00){}', 'TO_MATCH_ENVRCPT_SOME(0.00){}])']}}

mais pelo que percebo só funciona se for para uma string, no caso de um arquivo de varias linhas não funciona, pois o arquivo é uma lista se eu usar o 'readlines'.

Alguém consegue me dar um help para fazer isso funcionar em um arquivo de varias linhas e para salvar em um formato json.

Obs: Todas as linhas serão no mesmo padrão dessa string in line.

Desde já agradeço!

1 Resposta 1

2

Bom, acho que basta fazer uma iteração usando a lista do readlinescomo iterator mesmo. Veja:

import re

regexp = '(\]\ \[).*(\]\))'
pontuacao = '(\[-?\d*\.\d*\/)'
status = '(\:\s.\s\(.*\)\:\s)'
_id = '(\w{3}\:\s<\w*>,\s\w{2}:)'

f=open('stack.txt', 'r')
lines = f.readlines()
f.close()

dic={}
for line in lines:
    score = re.findall(pontuacao, line)[0].strip('[').strip('/')
    action = re.findall(status, line)[0].split('(')[1].strip('): ')
    qid = re.findall(_id, line)[0].split('<')[1].split('>')[0]
    signature = re.search(regexp, line).group().split(',')

    dic[qid] = {'score': score, 'action': action, 'metrics': signature}    

dic.keys()

Output:

dict_keys(['48H2js00X4zRj01', '60H2js00X4zRj01'])

NOTA: Eu criei um arquivo de txt copiei a linha que você usou como exemplo, alterando apenas o qid. O arquivo utilizado foi o seguinte (sem espaçamento, chamei de stack.txt):

rspamd_task_write_log: id: <CAP7Ane7t3GqwhbdnkcRiRD4vTR8wRVt=6yWTe7XYt6UC9yzjAQ@mail.test.com>, qid: <48H2js00X4zRj01>, ip: 209.85.208.65, from: <[email protected]>,\(default: F (no action): [-2.43/15.00] [IP_SCORE(-2.93){ip: (-2.32), ipnet: 123.123.123.0/17\(-4.14), asn: 15169(-3.28), country: US(-0.04);},SUSPICIOUS_RECIPS(1.50){},DMARC_POLICY_ALLOW(-0.50){test.com;none;},R_DKIM_ALLOW(-0.20){test.com:s=20161025;},R_SPF_ALLOW(\ -0.20){+ip4:123.123.123.0/17;},MIME_GOOD(-0.10){multipart/mixed;multipart/alternative;text/plain;},ARC_NA(0.00){},ASN(0.00){asn:15169, ipnet:123.123.123.0/17, country:US;},DKI\M_TRACE(0.00){test.com:+;},FROM_EQ_ENVFROM(0.00){},FROM_HAS_DN(0.00){},HAS_ATTACHMENT(0.00){},MIME_TRACE(0.00){0:+;1:+;2:+;3:~;4:~;},PREVIOUSLY_DELIVERED(0.00){test@\test.com;},RCPT_COUNT_GT_50(0.00){174;},RCVD_COUNT_TWO(0.00){2;},RCVD_TLS_ALL(0.00){},TAGGED_RCPT(0.00){},TO_DN_SOME(0.00){},TO_MATCH_ENVRCPT_SOME(0.00){}]), len: 65925\08, time: 379.997ms real, 201.985ms virtual, dns req: 23, digest: <3ea7e074fbea648462253b1522858d71>, rcpts: <[email protected]>, mime_rcpts: <test@test\l.com,[email protected],[email protected],...>

rspamd_task_write_log: id: <CAP7Ane7t3GqwhbdnkcRiRD4vTR8wRVt=6yWTe7XYt6UC9yzjAQ@mail.test.com>, qid: <60H2js00X4zRj01>, ip: 209.85.208.65, from: <[email protected]>,\(default: F (no action): [-2.43/15.00] [IP_SCORE(-2.93){ip: (-2.32), ipnet: 123.123.123.0/17\(-4.14), asn: 15169(-3.28), country: US(-0.04);},SUSPICIOUS_RECIPS(1.50){},DMARC_POLICY_ALLOW(-0.50){test.com;none;},R_DKIM_ALLOW(-0.20){test.com:s=20161025;},R_SPF_ALLOW(\ -0.20){+ip4:123.123.123.0/17;},MIME_GOOD(-0.10){multipart/mixed;multipart/alternative;text/plain;},ARC_NA(0.00){},ASN(0.00){asn:15169, ipnet:123.123.123.0/17, country:US;},DKI\M_TRACE(0.00){test.com:+;},FROM_EQ_ENVFROM(0.00){},FROM_HAS_DN(0.00){},HAS_ATTACHMENT(0.00){},MIME_TRACE(0.00){0:+;1:+;2:+;3:~;4:~;},PREVIOUSLY_DELIVERED(0.00){test@\test.com;},RCPT_COUNT_GT_50(0.00){174;},RCVD_COUNT_TWO(0.00){2;},RCVD_TLS_ALL(0.00){},TAGGED_RCPT(0.00){},TO_DN_SOME(0.00){},TO_MATCH_ENVRCPT_SOME(0.00){}]), len: 65925\08, time: 379.997ms real, 201.985ms virtual, dns req: 23, digest: <3ea7e074fbea648462253b1522858d71>, rcpts: <[email protected]>, mime_rcpts: <test@test\l.com,[email protected],[email protected],...>
1
  • muito obrigado pela ajuda amigo, realmente funcionou acho que errei na hora da indentação do código mais me ajudou muito amigo muito obrigado!
    – Léo B
    Commented 22/02/2020 às 16:07

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .