Python Forum
I want to log specific data to csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to log specific data to csv file
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 519 Jan-29-2024, 06:41 PM
Last Post: tester_V
  data validation with specific regular expression shaheen07 0 341 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Reading Specific Rows In a CSV File finndude 3 990 Dec-13-2022, 03:19 PM
Last Post: finndude
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,282 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,028 Mar-28-2022, 03:38 PM
Last Post: snippsat
  [Solved] Trying to read specific lines from a file Laplace12 7 3,552 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Extract specific sentences from text file Bubly 3 3,418 May-31-2021, 06:55 PM
Last Post: Larz60+
  Find specific file in an archive tester_V 8 3,479 Feb-13-2021, 06:13 PM
Last Post: tester_V
  xml file creation from an XML file template and data from an excel file naji_python 1 2,119 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Writing to file in a specific folder evapa8f 5 3,441 Nov-13-2020, 10:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020