I am cross referencing two data sources which share 6 common fields. The idea is the the marketing costs in file 1 are split out over the sales transactions in file 2. I've written a way to build a data structure from the first file so that the second one can access it quickly, but it seems un-pythonic to me. I'm interested to get some input and opinions on whether anyone thinks it could be written in a better way.
cost_matrix = {}
for line in marketing_costs:
line_date_object = time.strptime(line['date'], "%d/%m/%Y")
period = '%04d_%02d' % (line_date_object.tm_year, line_date_object.tm_mon)
territory = line['territory'].lower()
salesperson=line['salesperson'].lower()
customer_type = line['customer_type'].lower()
affiliate=line['affiliate'].lower()
product_group = line['product_group'].lower()
line_mktg_cost=line['mktg_cost']
try:
cost_matrix[period]
except KeyError:
cost_matrix[period]={}
try:
cost_matrix[period][territory]
except KeyError:
cost_matrix[period][territory]={}
try:
cost_matrix[period][territory][salesperson]
except KeyError:
cost_matrix[period][territory][salesperson]={}
try:
cost_matrix[period][territory][salesperson][customer_type]
except KeyError:
cost_matrix[period][territory][salesperson][customer_type]={}
try:
cost_matrix[period][territory][salesperson][customer_type][affiliate]
except KeyError:
cost_matrix[period][territory][salesperson][customer_type][affiliate]={}
try:
cost_matrix[period][territory][salesperson][customer_type][affiliate][product_group]
except KeyError:
cost_matrix[period][territory][salesperson][customer_type][affiliate][product_group]={}
cost_matrix[period][territory][salesperson][customer_type][affiliate][product_group]['mktg_cost']=0
cost_matrix[period][territory][salesperson][customer_type][affiliate][product_group]['mktg_cost']+=Decimal(line_mktg_cost)