[Tutor] String Attribute

Ltc Hotspot ltc.hotspot at gmail.com
Sun Aug 2 01:21:34 CEST 2015


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 <emile at 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.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
>
>
> _______________________________________________
> 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