I am working on a script that reads data from an excel workbook. The data is located in a named range whose first row contains the headers. It works!!! I find it superb that python is able to do such things!!!
Now my questions. a. My ranges can in practice be quite big, and although I am happy with the speed I get, because I am learning python, I think I could do it still a bit better. I think that the line for i in matriz[1:]: could be improved with an iterator/generator, but I am not quite certain how I should proceed. b. the following lines could be improved by means of a defaultdict, that I know because of a previous post. However you may come up with some other ideas (this b. question is not my main concern, you may discard it if you find the post too long) if i[a] and not(i[a] in g) and i[b]: g[i[a]] = i[b] elif i[a] and (i[a] in g) and i[b]: g[i[a]] += i[b] Can anybody provide some hints? Thank you for your cooperation. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + import win32com.client as win32 def agrupar(matriz, c1, c2): a, b = matriz[0].index(c1), matriz[0].index(c2) g = {} for i in matriz[1:]: if i[a] and not(i[a] in g) and i[b]: g[i[a]] = i[b] elif i[a] and (i[a] in g) and i[b]: g[i[a]] += i[b] for k in g: print '%-50s %15d' % (k, g[k]) # Abrir fichero excel xl = win32.gencache.EnsureDispatch('Excel.Application') ##xl.Visible = True wb=xl.Workbooks.Open(r'C:\Users\Vicente\Documents\VS\Python \Presupuesto fijos 10YP 2011-2020 V0.xls') # Obtención de datos datos=wb.Names('Datos').RefersToRange() print agrupar(datos, u'NomCC', u'BGT-09') # Cerrar libro de trabajo wb.Close() print -- http://mail.python.org/mailman/listinfo/python-list