def transform_csv(self):
        self.log(" Transforming source CSV")
        grouped = {}
        form2field = {
            # F460
            'A-1': 'itemized_monetary_contributions',
            'A-2': 'unitemized_monetary_contributions',
            'A-3': 'total_monetary_contributions',
            'F460-4': 'non_monetary_contributions',
            'F460-5': 'total_contributions',
            'E-1': 'itemized_expenditures',
            'E-2': 'unitemized_expenditures',
            'E-4': 'total_expenditures',
            'F460-16': 'ending_cash_balance',
            'F460-19': 'outstanding_debts',
            # F450
            'F450-7': 'total_monetary_contributions',
            'F450-8': 'non_monetary_contributions',
            'F450-10': 'total_contributions',
            'F450-1': 'itemized_expenditures',
            'F450-2': 'unitemized_expenditures',
            'E-6': 'total_expenditures',
        }
        self.log("  Regrouping")
        for r in csv.DictReader(open(self.source_csv, 'rb')):
            uid = "%s-%s" % (r['FILING_ID'], r['AMEND_ID'])
            formkey = "%s-%s" % (r['FORM_TYPE'], r['LINE_ITEM'])
            try:
                field = form2field[formkey]
            except KeyError:
                continue
            try:
                grouped[uid][field] = self.safeamt(r['AMOUNT_A'])
            except __HOLE__:
                grouped[uid] = SortedDict((
                    ("itemized_monetary_contributions", "\N"),
                    ("unitemized_monetary_contributions", "\N"),
                    ("total_monetary_contributions", "\N"),
                    ("non_monetary_contributions", "\N"),
                    ("total_contributions", "\N"),
                    ("itemized_expenditures", "\N"),
                    ("unitemized_expenditures", "\N"),
                    ("total_expenditures", "\N"),
                    ("ending_cash_balance", "\N"),
                    ("outstanding_debts", "\N")
                ))
                grouped[uid][field] = self.safeamt(r['AMOUNT_A'])
        self.log("  Writing to filesystem")
        out = csv.writer(open(self.target_csv, "wb"))
        outheaders = (
            "filing_id_raw",
            "amend_id",
            "itemized_monetary_contributions",
            "unitemized_monetary_contributions",
            "total_monetary_contributions",
            "non_monetary_contributions",
            "total_contributions",
            "itemized_expenditures",
            "unitemized_expenditures",
            "total_expenditures",
            "ending_cash_balance",
            "outstanding_debts"
        )
        out.writerow(outheaders)
        for uid, data in grouped.items():
            outrow = uid.split("-") + data.values()
            out.writerow(outrow)
dataset/ETHPy150Open california-civic-data-coalition/django-calaccess-campaign-browser/calaccess_campaign_browser/management/commands/loadcalaccesscampaignsummaries.py/Command.transform_csv