[Tutor] Dictionary Issue
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu Aug 6 00:23:11 CEST 2015
On 05/08/2015 15:15, Ltc Hotspot wrote:
> Hi everyone:
>
> I want to write a python program that reads through the data file of
> mbox-short.txt.Mbox-short.txt, i.e., download is available at
> http://www.py4inf.com/code/mbox-short.txt.
>
> Secondly, I want for python to figure out who sent the greatest number of
> mail messages.
>
> The output should read: cwen at iupui.edu 5
>
> However, there is a traceback message:
>
> In [40]: %run 9_4_4.py
> File "C:\Users\vm\Desktop\apps\docs\Python\_9_4_4.py", line 19
> count = dict()
> ^
> SyntaxError: invalid syntax
>
> Raw data code reads:
>
> fname = raw_input("Enter file name: ")
> handle = open (fname, 'r')
> text = handle.read()
>
> ## The program looks for 'From ' lines and takes the second
> ## word of those lines as the person who sent the mail.
>
> addresses = set()
> for addr in [ text.split()[2]('From ')
> if fromline
>
> ## The program creates a Python dictionary that maps
> ## the sender's mail address to a count of the number
> ## of times they appear in the file.
>
> count = dict()
> for wrd in word:
> count[wrd]= count.get(wrd,0) +1
>
> ## After the dictionary is produced, the program reads
> ## through the dictionary using a maximum loop to
> ## find the greatest number of mail messages.
>
> maxval = none
> maxkee = none
> for kee, val in count.items():
> if maxval == none or maxval <val:
> maxval = val
> maxkee = kee
>
> print items
>
> Question: What is the cause of the dictionary line message?
>
Learning to find these syntax errors is a skill you must learn for
yourself. Very often the error occurs one or even more lines before the
line on which the error is actually detected. So please go back from
where the error is actually reported and see what you can find. Very
strong hint, do not ask again until you've fixed at least two of the
three errors, and there may be more :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list