[Tutor] String Attribute

Alan Gauld alan.gauld at btinternet.com
Fri Jul 31 02:04:56 CEST 2015


On 30/07/15 22:17, ltc.hotspot at gmail.com wrote:

> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt" # assign fname
> fh=open(fname,'r') # Open a new file handle
> for line in fh:
>      print line
>      if 'From' in line.split()[0] and '@' in line: sender = line.split()[1]
>      fn.seek(0)
> print sender
>
> Questions: Why is the loop not repeating,

What makes you think so?
If you get an error(as I suspect) please post the entire error message.

I would expect a name error on the last line of the loop since there is 
no variable fn defined.

I don't know what you think the seek() is doing, but (assuming
you meant fh) it will reset the file to the first line each time
so you never finish the loop.

> and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008'
>
>  From stephen.marquard at uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch

Splitting on whitespace will ensure the bit you want is
in the second element


-- 
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