示例#1
0
文件: csv.py 项目: Valerich/import_me
 def header_row_offset(self) -> Optional[int]:
     index = None
     if self.header_row_index is not None:
         index = self.header_row_index
     elif self.first_data_row_index:
         index = self.first_data_row_index - 1
     if index is not None and index < 0:
         raise StopParsing('Invalid row index.')
     return index
示例#2
0
文件: xlsx.py 项目: WinaZar/import_me
    def validate_worksheet_headers(self, worksheet: Worksheet) -> None:
        expected_headers = {
            column.index: column.header.lower()
            for column in self.columns
            if column.header and column.validate_header
        }
        if expected_headers and self.header_row_offset is not None:
            for row in worksheet.iter_rows(min_row=self.header_row_offset + 1):
                columns = {
                    idx: col.value.strip().lower() if isinstance(
                        col.value, str) else col.value
                    for idx, col in enumerate(row) if idx in expected_headers
                }

                if columns != expected_headers:
                    file_path = self.file_path or 'file'
                    raise StopParsing(
                        (f'Incorrect column names in the file: {file_path}. '
                         f'Columns in file: {columns}. '
                         f'Expected columns: {expected_headers}.'))
                break
示例#3
0
文件: csv.py 项目: Valerich/import_me
    def validate_headers(self, reader: Iterator[List[str]]) -> None:
        expected_headers = {
            column.index: column.header.lower()
            for column in self.columns
            if column.header and column.validate_header
        }
        if expected_headers and self.header_row_offset is not None:
            for row_index, row in enumerate(reader):
                if row_index >= self.header_row_offset:
                    columns = {
                        idx:
                        col.strip().lower() if isinstance(col, str) else col
                        for idx, col in enumerate(row)
                        if idx in expected_headers
                    }

                    if columns != expected_headers:
                        file_path = self.file_path or 'file'
                        raise StopParsing((
                            f'Incorrect column names in the file: {file_path}. '
                            f'Columns in file: {columns}. '
                            f'Expected columns: {expected_headers}.'))
                    break