[Tutor] Dictionary Issue

Ltc Hotspot ltc.hotspot at gmail.com
Wed Aug 5 16:15:55 CEST 2015


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?


Regards,
Hal.


More information about the Tutor mailing list