On 22/04/2010 9:26 PM, Udai wrote:
> Hi,
>
> I am new to Python and this group. Sorry if repost.
Did you not read at least the postings of the last few days??
>
> I have to write a code that will save the automated regression
> results in a excel sheet on daily basis.
Why not use a database?
> I have use the xlwt package
> to create the excel sheet and save the results in excel sheet. To save
> the results in excel sheet I am using dictionary. Firstly I have
> stored all the values in the dictionary and then use this dictionary
> to store the results in the excel sheet.
A dictionary seems to be overkill ... isn't your data really just a
bunch of (timestamp, platform_id, debug_or_release_flag, something_else)
tuples?
> # Create workbook and worksheet
> wbk = xlwt.Workbook()
> sheet = wbk.add_sheet(wBookName, cell_overwrite_ok=True)
(1) Shouldn't that be wSheetName?
(2) It's not very efficient to use cell_overwrite_ok=True as a bludgeon
to overcome your evident requirement to eliminate duplicates and show
only the *last* result for each platform ... while ordering platforms by
*first* appearance (!) See suggested bludgeon avoidance technique below.
> # Create a font to use with the style
> font = xlwt.Font()
> font.bold = True
Get the tutorial, read the section on easyxf
>
> # Set the style's font to this new one you set up
> style = xlwt.XFStyle()
> style.font = font
>
> sheet.write(0,0,'Platform', style)
> sheet.write(0,1,'Debug', style)
> sheet.write(0,2,'Release', style)
>
> # Sorting the dictionary keys for better reporting
> sorted_keys = resultDict.keys()
> sorted_keys.sort()
>
=== replace all of this ...
> row = {}
> counter = 2
>
> # Generating row and column dict with values as serial numbers
> for key in sorted_keys:
> tmpValueList = resultDict[key]
> if tmpValueList[0] in row:
> continue
> else:
> row[tmpValueList[0]] = counter
> counter += 1
>
> coloum = {'Debug': 1, 'Release': 2}
>
> # Writing data into excel sheet
> for key in sorted_keys:
> dictValues = resultDict[key]
> sheet.write(row[dictValues[0]],0,dictValues[0])
>
> sheet.write(row[dictValues[0]],coloum[dictValues[1]],dictValues[2])
INDENTATION ERROR
=== with this:
row = {} # map platform -> row offset
row_data = [] # capture latest info for each platform
for timestamp in sorted_keys:
values = resultDict[timestamp]
platform = values[0]
if platform not in row:
row[platform] = len(row_data)
row_data.append(values)
else:
# replace earlier info with latest info
row_data[platform] = values
column = {'Debug': 1, 'Release': 2}
# In case you are short of memory, or just want to assert
# that you know what you are doing:
del row, resultDict, sorted_keys
for row_index, values in enumerate(row_data):
sheet.write(row_index+2, 0, values[0])
sheet.write(row_index+2, column[values[1]], values[2])
> logFileName = distName+'_'+projectName+'.xls'
> print ("Creating {excel} file at {path}" .format (excel =
> logFileName, path = os.getcwd()))
> wbk.save(logFileName)
> return logFileName
> The problem here is we need to run this script everyday and save the
> results. But the code is overwriting the old excel file and creating
> new excel file everyday. Hence the old data is lost.
> Can u please let me know is there any way so that instead of creating
> new file everyday we can append new excel sheet to already existing
> file.
Short answer: no. Long answer: read previous postings on this topic and
answers by others to your question.
HTH,
John