[Tutor] KeyError: '61.135.168.82' with dictionary.

Ray Parrish crp at cmc.net
Thu Mar 25 12:24:05 CET 2010


Hello,

The following code works interactively, but when ran from a script, I 
get the errors, after the second code block, which is the code from the 
script.

 >>> lineList = []
 >>> thisIPAddress = '61.135.168.82'
 >>> date = "2010 3 12"
 >>> dates = {date:{thisIPAddress:lineList}}
 >>> dates[date][thisIPAddress]
[]
 >>> dates[date][thisIPAddress].append(16)
 >>> dates[date][thisIPAddress]
[16]
 >>> dates[date][thisIPAddress].append(17)
 >>> dates[date][thisIPAddress]
[16, 17]
 >>>

Script code begins here -

          thisIPAddress = columns[10]
          lineList = []
          if date == "":
               date = columns[0]
               dates = {date:{thisIPAddress:lineList}}
               # For each date in the input file collect an array of log 
lines
               # for each unique ip address.
          date = columns[0]
          dates[date][thisIPAddress].append(eachLine)

Error messages follow -

Traceback (most recent call last):
  File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2519, in <module>
    ReadInDaysLog(tempLogName, countryCodes)
  File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2241, in 
ReadInDaysLog
    dates[date][thisIPAddress].append(eachLine)
KeyError: '61.135.168.82'


As you can see, it is tossing a key error on the same ip address that 
was used in the interactive code successfully,

I can not figure out what to change to make the script work. Does 
someone know why this happens?

Thanks, Ray Parrish

-- 
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com




More information about the Tutor mailing list