def handle_noargs(self, **options):
     for year in range(2001, 2016):
             source_file = '%s/energy/attf_v2_%s.csv' % (settings.LOCAL_DATA_ROOT, year)
             if os.path.isfile(source_file):
                 data_reader = csv.reader(open(source_file))
                 insert_count = 0
                 update_count = 0
                 for i, row in enumerate(data_reader):
                     if i == 0:
                         year_row = row
                     else:
                         for j,col in enumerate(row):
                             if j == 0:
                                 state = col.strip()
                             elif j > 0 and state <> '' and col.strip() <> '':
                                 year = year_row[j]
                                 try:
                                     record = AlternativeFuelVehicles.objects.get(state=state,year=year)
                                     if record.value <> clean_num(col):
                                         record.value = clean_num(col)
                                         record.save()
                                         db.reset_queries()
                                         update_count = update_count + 1
                                 except MultipleObjectsReturned:
                                     print '% error: multiple records exist for %s %s' % (source_file, year, state)
                                     continue
                                 except:
                                     record = AlternativeFuelVehicles(year = year, state=state, value = clean_num(col))
                                     record.save()
                                     db.reset_queries()
                                     insert_count = insert_count + 1
                 print '%s import complete. %s records updated, %s inserted' % (source_file, update_count, insert_count)
 def handle_noargs(self, **options):
     for year in range(2001, 2016):
         source_file = '%s/energy/attf_v2_%s.csv' % (
             settings.LOCAL_DATA_ROOT, year)
         if os.path.isfile(source_file):
             data_reader = csv.reader(open(source_file))
             insert_count = 0
             update_count = 0
             for i, row in enumerate(data_reader):
                 if i == 0:
                     year_row = row
                 else:
                     for j, col in enumerate(row):
                         if j == 0:
                             state = col.strip()
                         elif j > 0 and state <> '' and col.strip() <> '':
                             year = year_row[j]
                             try:
                                 record = AlternativeFuelVehicles.objects.get(
                                     state=state, year=year)
                                 if record.value <> clean_num(col):
                                     record.value = clean_num(col)
                                     record.save()
                                     db.reset_queries()
                                     update_count = update_count + 1
                             except MultipleObjectsReturned:
                                 print '% error: multiple records exist for %s %s' % (
                                     source_file, year, state)
                                 continue
                             except:
                                 record = AlternativeFuelVehicles(
                                     year=year,
                                     state=state,
                                     value=clean_num(col))
                                 record.save()
                                 db.reset_queries()
                                 insert_count = insert_count + 1
             print '%s import complete. %s records updated, %s inserted' % (
                 source_file, update_count, insert_count)
    def handle_noargs(self, **options):
        data_reader = csv.reader(open(SOURCE_FILE))
        first_year = 2003
        last_year = 2007
        total_years = last_year - first_year + 1

        #populate year array
        years = []
        for h in range(0, total_years):
            years.append(first_year + h)
        
        def clean_int(value):
            return int(value.replace(',', '').replace(' ', ''))
        
        for i, row in enumerate(data_reader):
            if i > 0:
                state = row[0]
                for j, col in enumerate(row):
                    if j > 0 and j < total_years + 1:
                        year = years[j-1]
                        value = clean_int(row[j])

                        record = AlternativeFuelVehicles(state=state, year=year, value=value)
                        record.save()