[Tutor] create an xls file using data from a txt file

Joel Goldstick joel.goldstick at gmail.com
Wed May 11 19:40:58 CEST 2011


On Wed, May 11, 2011 at 1:26 PM, James Reynolds <eire1130 at gmail.com> wrote:

> your "\" is a "/"
>
> when writing out a string, such as 'C:\test.xls', the "/" is an escape in
> python. So you have two choices, You can either write out path
> = 'C:\\test.xls', which will be 'C:\test.xls' or you can write out path =
> r'C:\test.xls' the "r" bit tells python that the following is a regular
> expression. or regex.
>

r means this is 'raw data'.  Take each character literally.  Raw data does
not use \ as a prefix to an escape code.  It is just another character

>
> You can also use Walter's method above.
>
> On Wed, May 11, 2011 at 1:10 PM, tax botsis <taxbotsis at gmail.com> wrote:
>
>> James,
>> how would you save the workbook into a specific directory? I tried to run
>> that:
>>
>> workbook.save('C:/test.xls')
>>
>> but I get the following error:
>>
>>
>> Traceback (most recent call last):
>>   File "<pyshell#50>", line 1, in <module>
>>     wbk.save("C:/test.xls")
>>   File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 634, in save
>>     doc.save(filename, self.get_biff_data())
>>   File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 615, in
>> get_biff_data
>>     self.__worksheets[self.__active_sheet].selected = True
>> IndexError: list index out of range
>>
>> Thanks
>> Tax
>>
>>
>> 2011/5/11 James Reynolds <eire1130 at gmail.com>
>>
>>> 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 <wprins at gmail.com> wrote:
>>>
>>>>
>>>>
>>>> On 11 May 2011 14:34, tee chwee liong <tcl76 at 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 at python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110511/79a9afd8/attachment.html>


More information about the Tutor mailing list