[Tutor] String Attribute

Emile van Sebille emile at fenx.com
Sun Aug 2 02:53:15 CEST 2015


On 8/1/2015 4:21 PM, Ltc Hotspot wrote:
> Hi Emile,
> Question: What is the source of the line 7 syntax: mbox.split?


I read mbox from the file.  eg,

mbox = open("mbox-short.txt",'r').read()

and it looks to me that if you insert the above in front of the for loop 
below you'll get further.

Emile


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




More information about the Tutor mailing list