[Tutor] String Attribute

Emile van Sebille emile at fenx.com
Sat Aug 1 23:18:29 CEST 2015


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.marquard at uct.ac.za
louis at media.berkeley.edu
zqian at umich.edu
rjlowe at iupui.edu
zqian at umich.edu
rjlowe at iupui.edu
cwen at iupui.edu
gsilver at umich.edu
gsilver at umich.edu
zqian at umich.edu
gsilver at umich.edu
wagnermr at iupui.edu
zqian at umich.edu
antranig at caret.cam.ac.uk
gopal.ramasammycook at gmail.com
david.horwitz at uct.ac.za
david.horwitz at uct.ac.za
stephen.marquard at uct.ac.za
louis at media.berkeley.edu
louis at media.berkeley.edu
ray at media.berkeley.edu
cwen at iupui.edu
cwen at iupui.edu
cwen at iupui.edu
 >>>



Emile



More information about the Tutor mailing list