Hi Alan,
I rewrote the code as follows:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
if not line.startswith('From'): continue
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
print line4
count = count + 1
print "There were", count, "lines in the file with From as the first word"
Question: How do I remove the duplicates:
stephen.marqu...@uct.ac.za
stephen.marqu...@uct.ac.za← Mismatch
lo...@media.berkeley.edu
lo...@media.berkeley.edu
zq...@umich.edu
zq...@umich.edu
rjl...@iupui.edu
rjl...@iupui.edu
Regards,
Hal
Sent from Surface
From: Alan Gauld
Sent: Thursday, July 30, 2015 5:04 PM
To: Tutor@python.org
On 30/07/15 22:17, ltc.hots...@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?
>>No count = count +1
If you get an error(as I suspect) please post the entire error message.
>>OK
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.
>>OK
> and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008'
>
> From stephen.marqu...@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
>>Check the revised code, above
--
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
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor