Slow day at work, so I tried something a little different mostly as a learning exercise for myself, let me know what you all think.
I thought it would be useful to have a writer that scales and that organizes the data. For example, you might have 20 tests one day, and 5 the next. I broke up the data into dictionaries where the IDs were keys, and everything that follows is a tuple of testX and result. Instead of iterating through each column, it only writes to the columns where data is present. Disclaimer: I am copying and pasting into Gmail, which sometimes screws up indents. I also put it into pastebin, which was pretty exciting considering I have never used it before: http://pastebin.com/2Dke5FtX import xlwt class Parser: ''' classdocs ''' def __init__(self, test, result): ''' Constructor ''' self.result = result self.test = test self.id = int(self.test[4:]) x = open('test.txt') id_dict = {} for all in x: y = all.split(" ") y[-1] = y[-1].strip() id_dict[y[0]] = y[1:] max_test = 0 for key, lists in id_dict.items(): length = len(lists)/2 a = 0 parser_list = [] for items in range(length): t = (lists[a], lists[a+1]) p = Parser(*t) parser_list.append(p) if max_test < p.id: max_test = p.id a +=2 id_dict[key] = parser_list workbook = xlwt.Workbook() worksheet = workbook.add_sheet("testruns", cell_overwrite_ok=True) header = 'TEST{0}' headers = ['ID'] range_id = range(max_test +1) for all in range_id[1:]: headers.append(header.format(all)) for i, colno in enumerate(headers): print i, type(i) worksheet.write(0, i, colno) rowno = 1 for keys, values in id_dict.items(): worksheet.write(rowno, 0, keys) for object_lists in values: worksheet.write(rowno, object_lists.id , object_lists.result) rowno +=1 workbook.save("test.xls") On Wed, May 11, 2011 at 9:58 AM, Walter Prins <wpr...@gmail.com> wrote: > > > On 11 May 2011 14:34, tee chwee liong <tc...@hotmail.com> wrote: > >> hi all, >> >> thanks for this sharing. when i copy and run this code, i got this error: >> >> Traceback (most recent call last): >> File "C:/Python25/myscript/excel/sampleexcel.py", line 1, in <module> >> import csv >> File "C:/Python25/myscript/excel\csv.py", line 3, in <module> >> w=csv.writer(open('output.csv','w')) >> AttributeError: 'module' object has no attribute 'writer' >> >> > Well, reading the error message, it's saying that module "csv", coming from > file "C:/Python25/myscript/excel\ > csv.py" has no member "writer". So, it seems you've called your test > script (module) "csv" which effecitvely hid the standard python "csv" > module. > > Try renaming your script file to something else ('testcsv.py' maybe) so its > name doesn't conflict with the standard "csv" module and try again. > > Walter > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor