On Wednesday 28 December 2005 10:18 am, Paul Kraus wrote: > I am trying to build a data structure that would be a dictionary of a > dictionary of a list. > > In Perl I would build the structure like so $dictionary{key1}{key2}[0] = X > I would iterate like so ... > foreach my $key1 ( sort keys %dictionary ) { > foreach my $key2 ( sort keys %{$dictionary{$key1}} ) { > foreach my $element ( @{$dictionary{$key1}{$key2} } ) { > print "$key1 => $key2 => $element\n"; > } > } > } > Here is the code that I used. Its functional and it works but there has got to be some better ways to do a lot of this. Transversing the data structure still seems like I have to be doing it the hard way.
The input data file has fixed width fields that are delimited by pipe. So depending on the justification for each field it will either have leading or ending whitespace. TIA, Paul <code> #!/usr/bin/python ############################# ## Paul D. Kraus - 2005-12-27 ## parse.py - Parse Text File ## Pipe deliminted '|' ############################# ## Fields: CustCode [0] ## : OrdrType [1] ## : OrdrReturn [2] ## : State [3] ## : QtyShipped [4] ## : VendCode [5] ## : InvoiceDate [7] ############################# import re import string results = {} def format_date(datestring): (period,day,year) = map(int,datestring.split('/') ) period += 2 if period == 13: period = 1; year += 1 if period == 14: period = 2; year += 1 if year > 80: year = '19%02d' % year else: year = '20%02d' % year return (year,period) def format_qty(qty,credit,oreturn): qty = float(qty) if credit == 'C' or oreturn == 'Y': return qty * -1 else: return qty textfile = open('orders.txt','r') for line in textfile: fields = map( string.strip, line.split( '|' ) ) fields[4] = format_qty(fields[ 4 ],fields[ 1 ], fields[ 2 ] ) (year, period) = format_date( fields[7] ) for count in range(12): if count == period: if results.get( ( year, fields[6], count), 0): results[ year,fields[6], count] += fields[4] else: results[ year,fields[6],count] = fields[4] sortedkeys = results.keys() sortedkeys.sort() for keys in sortedkeys: res_string = keys[0]+'|'+keys[1] for count in range(12): if results.get((keys[0],keys[1],count),0): res_string += '|'+str(results[keys[0],keys[1],count]) else: res_string += '|0' print res_string </code> _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor