[Tutor] String Attribute

ltc.hotspot at gmail.com ltc.hotspot at gmail.com
Fri Jul 31 02:25:26 CEST 2015











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.marquard at uct.ac.za
stephen.marquard at uct.ac.za← Mismatch
louis at media.berkeley.edu
louis at media.berkeley.edu
zqian at umich.edu
zqian at umich.edu
rjlowe at iupui.edu
rjlowe at iupui.edu









Regards,

Hal


Sent from Surface





From: Alan Gauld
Sent: ‎Thursday‎, ‎July‎ ‎30‎, ‎2015 ‎5‎:‎04‎ ‎PM
To: Tutor at python.org





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?




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




>>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 at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list