Search code examples
pythonpython-2.6defaultdict

Iter through defaultdict of defaultdict on Python 2.6


In order to have the number of failed first requests of different urls from an access log with Python, I had to create a defaultdict of defaultdict in this way:

apache_status_dict = defaultdict(lambda : defaultdict(int))

I count the number of failed requests using an apachelog parser:

for index, line in enumerate(open(path+aFile)):    
   if int(str(data['%>s'])[0]) == 4 or int(str(data['%>s'])[0]) == 5:
        apache_status_dict[data['%{Referer}i']][data['%r']] += 1

The server I'm working on have Python 2.6 installed and there is no way to update it. So is there any way to make something like this in my code :

for url in apache_status_dict:
  for req in url:
     <code...>

Any help or suggestion would be appreciated !


Solution

  • for url in apache_status_dict.keys():
      for req in apache_status_dict[url]:
          print apache_status_dict[url][req]