Nested dictionaries trouble

IamIan iansan at gmail.com
Wed Apr 11 14:57:56 EDT 2007


Hello,

I'm writing a simple FTP log parser that sums file sizes as it runs. I
have a yearTotals dictionary with year keys and the monthTotals
dictionary as its values. The monthTotals dictionary has month keys
and file size values. The script works except the results are written
for all years, rather than just one year. I'm thinking there's an
error in the way I set my dictionaries up or reference them...

import glob, traceback

years = ["2005", "2006", "2007"]
months = ["01","02","03","04","05","06","07","08","09","10","11","12"]
# Create months dictionary to convert log values
logMonths =
{"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May":"05","Jun":"06","Jul":"07","Aug":"08","Sep":"09","Oct":"10","Nov":"11","Dec":"12"}
# Create monthTotals dictionary with default 0 value
monthTotals = dict.fromkeys(months, 0)
# Nest monthTotals dictionary in yearTotals dictionary
yearTotals = {}
for year in years:
  yearTotals.setdefault(year, monthTotals)

currentLogs = glob.glob("/logs/ftp/*")

try:
  for currentLog in currentLogs:
    readLog = open(currentLog,"r")
    for line in readLog.readlines():
      if not line: continue
      if len(line) < 50: continue
      logLine = line.split()

      # The 2nd element is month, 5th is year, 8th is filesize
      # Counting from zero:

      # Lookup year/month pair value
      logMonth = logMonths[logLine[1]]
      currentYearMonth = yearTotals[logLine[4]][logMonth]

      # Update year/month value
      currentYearMonth += int(logLine[7])
      yearTotals[logLine[4]][logMonth] = currentYearMonth
except:
  print "Failed on: " + currentLog
  traceback.print_exc()

# Print dictionaries
for x in yearTotals.keys():
  print "KEY",'\t',"VALUE"
  print x,'\t',yearTotals[x]
  #print "  key",'\t',"value"
  for y in yearTotals[x].keys():
    print "  ",y,'\t',yearTotals[x][y]


Thank you,
Ian




More information about the Python-list mailing list