Python While loop Takes too much time.

marco.nawijn at colosso.nl marco.nawijn at colosso.nl
Mon Jun 30 14:34:13 EDT 2014


On Monday, June 30, 2014 1:32:23 PM UTC+2, Jaydeep Patil wrote:
> I have did excel automation using python.
> 
> In my code I am creating python dictionaries for different three columns data at a time.There are are many rows above 4000. Lets have look in below function. Why it is taking too much time?
> 
> 
> 
> Code:
> 
> 
> 
> def transientTestDict(self,ws,startrow,startcol):
> 
>         
> 
>         self.hwaDict = OrderedDict()
> 
>         self.yawRateDict = OrderedDict()
> 
>         
> 
>         rng = ws.Cells(startrow,startcol)
> 
>         
> 
>         while not rng.Value is None:
> 
>             r = rng.Row
> 
>             c = rng.Column
> 
>             
> 
>             time = rng.Value
> 
>             
> 
>             rng1 = rng.GetOffset(0,1)
> 
>             hwa = rng1.Value
> 
>             
> 
>             rng2 = rng.GetOffset(0,2)
> 
>             yawrate = rng2.Value
> 
>             
> 
>             self.hwaDict[time] = hwa,rng.Row,rng.Column
> 
>             self.yawRateDict[time] = yawrate,rng.Row,rng.Column
> 
>             
> 
>             rng = ws.Cells(r+1,c)
> 
> 
> 
> 
> 
> 
> 
> Please have look in above code & suggest me to improve speed of my code.
> 
> 
> 
> 
> 
> 
> 
> Regards
> 
> Jaydeep Patil

Hi Jaydeep,

I agree with Peter. I would avoid moving from cell to
cell through the EXCEL interface if you can avoid.

If possible, I would try to read ranges from EXCEL into
a python list (or maybe numpy arrays) and do the processing
in Python. In the past I even dumped an EXCEL sheet as a
CSV file and then used the numpy recfromcsv function to 
process the data.

If you are really brave, dump EXCEL alltogether :) and do
all the work in Python (have you already tried IPython 
notebook?).

Regards,

Marco




More information about the Python-list mailing list