def __do_import_csv(self, headers): self.csv_file.seek(0) reader = UnicodeReader(self.csv_file, encoding=self.encoding) self.headers_dict = dict((h, i) for i, h in enumerate(headers)) # checks that required columns are presents for field in self.REQUIRED_HEADERS: if field not in self.headers_dict: raise CSVImportError({1: self.get_missing_headers_msg()}) # read the header reader.next() self._errors = defaultdict(ErrorList) self.objects = [] # parse each row for line, row in enumerate(reader): try: self.parse_row(line + 2, row) except Exception, e: self.store_errors(line + 2, e)
def __init__(self, csv_file, encoding, known_headers): reader = UnicodeReader(csv_file, encoding=encoding) self.headers = reader.next() self.guessed_headers = self._guess_headers(known_headers) self.rows = tuple(islice(reader, 2))