import json import os import inquirer import pathlib # get current dir path dir_path = pathlib.Path().resolve() res = [] # iterazione nella directory for path in os.listdir(dir_path): # controllo se il file seguente รจ una directory if os.path.isfile(os.path.join(dir_path, path)): res.append(path) # creazione oggetto inquirer files = [ inquirer.List('file', message="Seleziona il file", choices=res, ), ] #stampa scelte sul terminale answers = inquirer.prompt(files) print(answers["file"]) # nFileJSON = input("Inserire nome file di scrittura: ") nFileJSON = answers["file"].replace("csv","json") doRun = bool(True) # --------------------------------- # SETUP # --------------------------------- # indica numero di bit x banco allarmi numBit = 16 sepChar=';' #',' maxBlock=32 # num max blocchi da leggere # --------------------------------- n1 = 0 n2 = numBit index = 0 with open(answers["file"], 'r', encoding='utf-8') as fp: lines = fp.readlines(); fopen = open(nFileJSON, "a", encoding='utf-8') fopen.write("[") while doRun: x = lines[n1:n2] alarmsList = [] i = 0 alarms="" for k in x: # gestione stringhe alarms = x[i].split(sepChar)[4].replace("\n", "") group = x[i].split(sepChar)[2].replace("\n", "") if not alarms or alarms =='': alarms = alarms.replace("", "##") if not group or group == "": group = group.replace("", " ") alarmsList.append(alarms) i= i + 1 # Creazione oggetto json data = { 'description' : group + ' ', 'tipoMem' : 'Byte', 'memAddr' : group, 'index' : index, 'size' : int(numBit/8), 'messages' : tuple(alarmsList) } # Serializzazione json json_object = json.dumps(data, indent=4) # Scrittura nel file fopen.write("\r" + json_object + ",\r") n1 = n1 + numBit n2 = n2 + numBit index = index+1 # doRun script if x == "" or index > maxBlock: doRun = bool(False) fopen.write("]")