[Tutor] Formatting text file with python

Hüseyin Ertuğrul huseyin at piramit.com.tr
Tue Feb 6 16:07:24 EST 2018


Hello friends,
I want to format the log (text) file my email's server.
The text file (named s1.txt) contains the following information, text file has about 3000 lines.

"hMailServer SpamProtection rejected RCPT (Sender: Valeria0021 at mikelsonconstruction.com, IP:187.62.63.218, Reason: Rejected by Spamhaus.)"
"hMailServer SpamProtection rejected RCPT (Sender: Veronika07372 at etb.net.co, IP:190.25.189.74, Reason: Rejected by SpamCop.)"
"hMailServer SpamProtection rejected RCPT (Sender: Sofia610 at pembroketownhouse.ie, IP:103.247.48.95, Reason: Rejected by Spamhaus.)"

I want to select ip addresses in text file and I want to delete duplicated records. Finally I want to write this data into a new file. What do you suggest me?

That codes returned me about 500 records and gives error ;

Traceback (most recent call last):
  File "C:/Depo/Python/prj1/analiz.py", line 17, in <module>
    prnt(deger)
  File "C:/Depo/Python/prj1/analiz.py", line 7, in prnt
    iplist = list_line[1]
IndexError: list index out of range)
-------------------------------------------------------------------------------
My code is below;
def prnt(L1):

    L1 = L1[:-1]

    list_line = L1.split(",")
    list0 = list_line[0]
    iplist = list_line[1]
    list2 = list_line[2]
    print(iplist)


with open("s1.txt","r",) as file:

    for deger in file:
        prnt(deger)

#with open("iplistesi.txt","w") as file2:
#    file2.write(i)


Best Regards.

Huseyin



More information about the Tutor mailing list