Files

79 lines
1.8 KiB
Python

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: ")
stop = bool(True)
n1 = 0
n2 = 8
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 stop:
x = lines[n1:n2]
alarmsList = []
i = 0
alarms=""
for k in x:
# gestione stringhe
alarms = x[i].split(',')[4].replace("\n", "")
splitLines = x[i].split(';')
group = splitLines[1]
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 + ' ' + 'Byte_B'+str(index),
'tipoMem' : 'Byte',
'memAddr' : splitLines[2],
'index' : index,
'size' : 1,
'messages' : tuple(alarmsList)
}
# Serializzazione json
json_object = json.dumps(data, indent=4)
# Scrittura nel file
fopen.write("\r" + json_object + ",\r")
n1 = n1 + 8
n2 = n2 + 8
index = index+1
# Stop script
if splitLines[2] == "n/a":
stop = bool(False)
fopen.write("]")