Hi Emile, Question: What is the source of the line 7 syntax: mbox.split? Here is a copy of the Traceback message: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\8_5_v_26.py in <module>() 5 addresses = set() 6 for addr in [ fromline.split()[0] ----> 7 for fromline in mbox.split('From ') 8 if fromline ]: 9 count = count + 1 NameError: name 'mbox' is not defined
Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for addr in [ fromline.split()[0] for fromline in mbox.split('From ') if fromline ]: count = count + 1 print addr print "There were", count, "lines in the file with From as the first word" Regards, Hal On Sat, Aug 1, 2015 at 2:18 PM, Emile van Sebille <em...@fenx.com> wrote: > On 8/1/2015 12:00 PM, Ltc Hotspot wrote: > >> Hi Everyone: >> >> >> Let me repost the question: >> >> You will parse the From line using split() and print out the second word >> in >> the line (i.e. the entire address of the person who sent the message). >> Then >> print out a count at the end. >> >> *Hint:* make sure not to include the lines that start with 'From:'. >> >> You can download the sample data at >> http://www.pythonlearn.com/code/mbox-short.txt >> > > Cool - thanks. That's an mbox file. > > Can you explain the apparent dichotomy of the question directing you to > 'parse the from line' and the hint? I'm going to guess they mean that > you're not to print that line in the output? Aah, I see -- there're two > different lines that start From -- both with and without a trailing colon. > So then, we can split on 'From ' and recognizing the split eats the > split-on portion > > >>> '1234567'.split('4') > ['123', '567'] > > ... and leaves an empty entry when splitting on the first characters of > the line > > >>> '1234567'.split('1') > ['', '234567'] > > ... we get to: > > for addr in [ fromline.split()[0] > for fromline in mbox.split('From ') > if fromline ]: > print addr > > stephen.marqu...@uct.ac.za > lo...@media.berkeley.edu > zq...@umich.edu > rjl...@iupui.edu > zq...@umich.edu > rjl...@iupui.edu > c...@iupui.edu > gsil...@umich.edu > gsil...@umich.edu > zq...@umich.edu > gsil...@umich.edu > wagne...@iupui.edu > zq...@umich.edu > antra...@caret.cam.ac.uk > gopal.ramasammyc...@gmail.com > david.horw...@uct.ac.za > david.horw...@uct.ac.za > stephen.marqu...@uct.ac.za > lo...@media.berkeley.edu > lo...@media.berkeley.edu > r...@media.berkeley.edu > c...@iupui.edu > c...@iupui.edu > c...@iupui.edu > >>> > > > > Emile > > > _______________________________________________ > 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