Created
May 16, 2020 12:38
-
-
Save jmoreno/47eef2815dbf37a8ef32a1a760bdc5f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| def main(): | |
| # estructura del fichero de microdatos de la DGT y creación de lista con las partes que lo componen | |
| widths = 8,1,8,30,22,1,21,2,1,5,6,6,6,3,2,2,2,2,24,2,2,1,8,5,8,1,1,9,3,5,30,7,3,5,1,1,1,1,1,1,11,25,25,35,70,6,6,4,4,3,8,4,4,4,6,30,50,35,25,35,4,4,4,1,25,1,4,25,8 | |
| slices = [] | |
| offset = 0 | |
| for w in widths: | |
| slices.append(slice(offset, offset + w)) | |
| offset += w | |
| # estructura del VIN y creación de lista con las tres partes que lo componen | |
| vin_widths = 3,6,8 | |
| vin_slices = [] | |
| offset = 0 | |
| for vw in vin_widths: | |
| vin_slices.append(slice(offset, offset + vw)) | |
| offset += vw | |
| # campos del fichero de microdatos que vamos a utilizar en el fichero de salida | |
| fields = 2,4,5,7,8,9,10,11,22,32,42,43,44,45 | |
| fieldsname = 'COD_CLASE_MAT', 'MARCA_ITV', 'MODELO_ITV', 'BASTIDOR_ITV', 'COD_TIPO', 'COD_PROPULSION_ITV', 'CILINDRADA_ITV', 'POTENCIA_ITV', 'CLAVE_TRAMITE', 'KW_ITV', 'TIPO_ITV', 'VARIANTE_ITV', 'VERSION_ITV', 'FABRICANTE_ITV', 'WMI', 'VDS', 'VIS' | |
| f=open("./vin/export_mensual_mat_202001.txt", encoding="latin-1") | |
| # ignoramos la primera fila del registro | |
| fl =f.readlines()[1:] | |
| with open('output_vin.csv', mode="w") as output_vin_file: | |
| output_writer = csv.writer(output_vin_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
| output_writer.writerow(fieldsname) | |
| for x in fl: | |
| output_fields = [] | |
| pieces = [x[slice] for slice in slices] | |
| for field in fields: | |
| output_fields.append(pieces[field-1]) | |
| vin_pieces = [pieces[6][slice] for slice in vin_slices] | |
| for vin_piece in vin_pieces: | |
| output_fields.append(vin_piece) | |
| # solo sacamos los registros que tienen VIN (BASTIDOR) | |
| if len(pieces[6].strip()) >= 17: | |
| output_writer.writerow(output_fields) | |
| if __name__== "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment