How to make tree of dictionaries?

Vlad Sirenko zcoder at bigmir.net
Tue Aug 12 17:47:17 EDT 2003


I need:
dict = {2002 : {'Jan': {1 : 'num1', 2: 'num2', 3 : 'num3'}, 
               {'Feb': {1 : 'num4', 2: 'num5', 3 : 'num6'} } }
        2003 : {'Jan': {1 : 'num7', 2: 'num8', 3 : 'num9'} } }

How to do it programmatically?
In Perl I would do something like:

while ($line = <>) {
  if ($line =~ /^---\s+\w+\s+(\w+)\s+(\d*)\s+(\d+):(\d+):(\d+)\s+(\d+)\s+---$/) {
	($month,$date,$hour,$minute,$sec,$year) = ($1,$2,$3,$4,$5,$6);
	$statistics->{$year}->{$month}->{$date} += $sec;
  }
}

But how to do it in Python without catching 'KeyError' exception or
iterating over some nested loops. How to do it elegantly?

-- 
WBR, VS





More information about the Python-list mailing list