[Tutor] Formatting text file with python

Alan Gauld alan.gauld at yahoo.co.uk
Wed Feb 7 03:55:38 EST 2018


On 06/02/18 21:07, Hüseyin Ertuğrul wrote:

> "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.)"
> 
> 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)

So you have a line with no commas in it. Have you checked
the line that causes the error - presumably right after
the last one that printed correctly.

> -------------------------------------------------------------------------------
> My code is below;
> def prnt(L1):
> 
>     L1 = L1[:-1]
> 
>     list_line = L1.split(",")

You are splitting by comma but I notice the IP addresses
all start with IP: so it might be easier/more reliable
to split by 'IP:' in case there are other messages with
commas but no IP addresses in them?

But even so you still need to either check the result
length or use a try/except IndexError to deal with malformed
lines.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list