def load_files(self): charset = utils.ask_for_charset() if not charset: raise utils.UserInterruptError() filename = utils.select_files(only_one_file=True, extension=ru(QCoreApplication.translate('GeneralCsvImportGui', "Comma or semicolon separated csv file %s;;Comma or semicolon separated csv text file %s;;Comma or semicolon separated file %s"))%('(*.csv)', '(*.txt)', '(*.*)')) if isinstance(filename, (list, tuple)): filename = filename[0] filename = ru(filename) delimiter = utils.get_delimiter(filename=filename, charset=charset, delimiters=[',', ';']) self.file_data = self.file_to_list(filename, charset, delimiter) header_question = utils.Askuser(question="YesNo", msg=ru(QCoreApplication.translate('GeneralCsvImportGui', """Does the file contain a header?"""))) utils.start_waiting_cursor() if header_question.result: # Remove duplicate header entries header = self.file_data[0] seen = set() seen_add = seen.add remove_cols = [idx for idx, x in enumerate(header) if x and (x in seen or seen_add(x))] self.file_data = [[col for idx, col in enumerate(row) if idx not in remove_cols] for row in self.file_data] self.table_chooser.file_header = self.file_data[0] else: header = ['Column ' + str(colnr) for colnr in range(len(self.file_data[0]))] self.table_chooser.file_header = header self.file_data.reverse() self.file_data.append(header) self.file_data.reverse() utils.stop_waiting_cursor()
def _test(filename, mock_iface, mock_delimiter_question): mock_delimiter_question.return_value = (',', True) delimiter = utils.get_delimiter(filename, 'utf-8') assert delimiter == ','