import csv
import json

file="tubi da 9 a 24 gen 2023"
with open(f"tmp/{file}.csv",) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')

    with open(f'tmp/{file}_out.csv', 'w') as csv_out:

        # create the csv writer
        writer = csv.writer(csv_out, lineterminator="\n")

        line_count = 0
        for row in csv_reader:
            if line_count == 0:
                print(f'Column names are {", ".join(row)}')
                row.append("res misurata")
                row.append("res attesa")
                row.pop(1)  # remove data column
                line_count += 1
                writer.writerow(row)
            else:
                data=json.loads(row[1])
                if "resistance" in data.keys():
                    res=data["resistance"]["0"]["results"]["result"]
                    exp=data["resistance"]["0"]["step"]["spec"]["expected"]
                    resstr=f'{res:.3f}'
                    expstr = f'{exp:.3f}'
                    print(f'res={resstr} exp={expstr}')
                    line_count += 1
                    row.append(resstr)
                    row.append(expstr)
                    row.pop(1) # remove data column
                    writer.writerow(row)

        print(f'Processed {line_count} lines.')