def __init__(self, teryt_file_name): """ Initiates teryt decoder using csv_file with teryt codes. Arguments: teryt_file_name - name of csv file containing TERYT codes """ csv_file = CsvFile(teryt_file_name, delim=';', quote='"') data = CsvData(csv_file) data.build(rows_type='list') self.codes = {} for row in data.get_rows(): if not self.is_important(row[4]): continue woj, pow, gm = row[1], row[2], row[3] name, type = row[5], row[6] if woj not in self.codes: self.codes[woj] = {'name': name, 'type': type, 'pows': {}} if pow != '00': powiats = self.codes[woj]['pows'] if pow not in powiats: powiats[pow] = {'name': name, 'type': type, 'gms': {}} if gm != '00': gmins = powiats[pow]['gms'] gmins[gm] = {'name': name, 'type': type} csv_file.close()
def decode_file(self, source_filename, result_filename): """Decodes given file. Arguments: source_filename -- name of file to decode result_filename -- name of file to save decoded data """ csv_file = CsvFile(source_filename, delim=';', quote='"') csv_data = CsvData(csv_file) new_header = self.decode_header(csv_data.get_header()) new_rows = [] row = csv_data.get_next_row(row_type='list') i = 0 while row: i += 1 changed_row = row[:] is_jst = row[4] in ['z', 'Z'] changed_row[1] = self.teryt_decoder.get_name(row[1]) if changed_row[1] is None: print i if is_jst: changed_row[2] = self.teryt_decoder.get_name(row[1] + row[2]) if changed_row[2] is None: print i changed_row[3] = self.jst_decoder.get_name(row[6][1:]) # decoder has xyz, file has 0xyz if changed_row[3] is None: print i changed_row[4] = u'Związek JST' else: type = self.teryt_decoder.get_type(row[1]) if row[2] != '00': changed_row[2] = self.teryt_decoder.get_name(row[1] + row[2]) if changed_row[2] is None: print i type = self.teryt_decoder.get_type(row[1] + row[2]) else: changed_row[2] = '' if row[3] != '00': changed_row[3] = self.teryt_decoder.get_name(row[1] + row[2] + row[3]) if changed_row[3] is None: print i type = self.teryt_decoder.get_type(row[1] + row[2] + row[3]) else: changed_row[3] = '' changed_row[4] = type self.clean_row(changed_row) new_rows.append(changed_row) row = csv_data.get_next_row(row_type='list') csv_file.close() new_data = Data([new_header] + new_rows, result_filename) new_data.save()