import csv import re # open the file in the write mode f = open('resultsParsed_UtilizationDifferent_40rep_DeviceOnlyVar_2replicas_Std30_RequestDeviceRandom.csv', 'w',newline='') # create the csv writer writer = csv.writer(f) # Header writer.writerow(['Repetition','Scenario','Alternative','Device0','Device1','Device2','Device3','Device4','Device5', 'Device6','Device7','Device8','Device9','Device10']) nextline= False nextlineUtilization = False i=0 saveDevicePlacement = True with open("40rep_DeviceOnlyVar_2replicas_Std30_RequestDeviceRandom.txt","r") as fi: for ln in fi: if nextlineUtilization: #This is a line with the data for the utilization scenario gatewayList=[s for s in re.findall(r'[\s\{]gateway_(\d+)=(\d+.\d+)', ln)] utilizationInfo=[0.0]*11 for infoPair in gatewayList: #A pair has the form (deviceNumber,utilizationValue) utilizationInfo[int(infoPair[0])]=infoPair[1] #print(utilizationInfo) # Reset nextlineUtilization= False if ln.startswith("Repetition"): # Get the repetition number repetition =[s for s in re.findall(r'\b\d+\b', ln)] if ln.startswith("Utilization scenario"): # Get the scenario number number =[s for s in re.findall(r'\b\d+\b', ln)] nextlineUtilization=True if nextline: #This is a line with the name of the alternative if ln.startswith("OVERALL OPT"): # Get the scenario number alternative ="Overall" saveDevicePlacement=False # The results indicate the utilization information only for the devices used in the placement - save temporarily overall results results2=[] for deviceNb in range(11): if deviceNb in devicesUsedInPlacement: results2.append(utilizationInfo[deviceNb]) else: results2.append('NA') #print(results) if ln.startswith("MARGINAL OPT"): # Get the scenario number alternative ="Marginal" # Assuming Marginal is always second saveDevicePlacement=True if not devicesUsedInPlacement==devicesUsedInPlacementSaved: #Only save info when placement is different print("Different placement, writing to CSV") # The results indicate the utilization information only for the devices used in the placement results=[] for deviceNb in range(11): if deviceNb in devicesUsedInPlacement: results.append(utilizationInfo[deviceNb]) else: results.append('NA') #print(results) # Write to the CSV file - the two rows writer.writerow([repetition[0],number[0],"Overall",results2[0],results2[1],results2[2],results2[3],results2[4],results2[5],results2[6],results2[7],results2[8],results2[9],results2[10]]) writer.writerow([repetition[0],number[0],alternative,results[0],results[1],results[2],results[3],results[4],results[5],results[6],results[7],results[8],results[9],results[10]]) i=i+2 print(i) # Reset nextline= False if ln.startswith("Objective"): # Get the devices that are part of the resulting placement devicesUsedInPlacement=[int(s) for s in re.findall(r'yg(\d+)\w\w=1', ln)] print(devicesUsedInPlacement) if (saveDevicePlacement): devicesUsedInPlacementSaved=devicesUsedInPlacement # Get to the nextline - where the alternative is written nextline=True if ln.startswith("The request is infeasible"): # Get the devices that are part of the resulting placement - none in this case devicesUsedInPlacement=[] print(devicesUsedInPlacement) if (saveDevicePlacement): devicesUsedInPlacementSaved=devicesUsedInPlacement # Get to the nextline - where the alternative is written nextline=True # close the file f.close()