I want to log specific data to csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I want to log specific data to csv file (/thread-23136.html) |
I want to log specific data to csv file - akshay3210 - Dec-12-2019 Hi, I'm very new to python. Kindly Help me with this problem. I have the script which logs for GetAllVars to csv file . But I need to log the data for GetAllTestStepResults not for GetAllVars. Here is the script below def GetAllVars(path): uut = {} vars = {} file = open(path,'r') lines = file.readlines() ii = 0 while ii < len(lines): line = lines[ii] # <V s='2'/>Name,Value start = line.find("<V") while start > -1: # Next StartToken ('<') is the end of this element end = line.find("<", start + 1) if end > -1: # <V s='2'/>Name,Value var = line[start:end] # Get VarName and Value tokens = var.split('>') tokens = tokens[1].split(',') # vars[name] = value vars[tokens[0]]=tokens[1] # Keep reading while the next element is a variable <V s='2'/> tag = line[end+1:end+2] if tag == "V": start = end + 1 else: start = -1 uut[ii] = vars vars = {} ii = ii + 1 return uut def GetAllTestStepResults(path): uut = {} results = {} file = open(path, 'r') # UUT elements (<UUT>,...,</UUT>) are terminated by "\r\n" lines = file.readlines() ii = 0 while ii < len(lines): line = lines[ii] start = line.find("<S ") while start > -1: # Next StartToken ('<') marks the end of this element. end = line.find("<", start + 1) if end > -1: # <S t='' s='' .../>Name,Value1,Value2,..., step = line[start:end] # Ignore action step results if((step.find("t='b'") == -1) and (step.find("c='LOG'") == -1)): # Get TestName, Data, and Status tokens = step.split('>') tokens = tokens[1].split(',') # tokens[0] -> TestName # tokens[1] -> Data (number, string or boolean) # tokens[2] -> Status ("Passed", "Failed", "Skipped", "Error", "Terminated") # results[TestName] = [Data, Status] results[tokens[0]]=[tokens[1], tokens[2]] start = line.find("S ", end) uut[ii] = results results = {} # Get next UUT element (<UUT>,...,</UUT>) ii = ii + 1 return uut # print("Get All Test UUT Variables:") # uut = GetAllVars("all.txt") # for ii, vars in uut.items(): # print("UUT[" + str(ii) + "]") # for name, data in vars.items(): # print("\t" + name + "=" + data) import csv with open('large.csv','w') as f1: print("Get All Test UUT Variables:") uut = GetAllVars("all.txt") for ii, vars in uut.items(): print("UUT[" + str(ii) + "]") writer=csv.writer(f1) for name, data in vars.items(): row = (name,data) writer.writerow(row) import pandas as pd df = pd.read_csv("large.csv", header= None) df.columns = ['info', 'value'] df1 = (df.set_index([df['info'].eq('Profile').cumsum(), 'info'])['value'] .unstack() .rename_axis(None, axis=1)) df1.to_csv("automated.csv") print("Get All Test Step Results:") uut = GetAllTestStepResults("all.txt") for ii, results in uut.items(): print("UUT[" + str(ii) + "]") for name, result in results.items(): print("\t" + name + " {Data=" + result[0] + " Status=" + result[1] + "}")Eagerly waiting for the answer. Thank you |