コード例 #1
0
 def tableToDictionary(self,currSheet, rowIndex,colIndex):
     #returns a Cell having endColX and endColY
     
     dictColumns = {} # for checking duplicaton if column is duplicated throw exception
     arrColumns = [] # for making the json.
     cellValue = Util.cellVal(currSheet, rowIndex, colIndex) # initialize 
     
     while (colIndex < currSheet.ncols and cellValue != ""): # read the row till empty cell is found it is ok to put a condition on empty cell 
         cellValue = Util.cellVal(currSheet, rowIndex, colIndex)
         
         field = None
         
         if(cellValue == Rules.idColumnName):
             field = MField()
             field.fieldName = Rules.idColumnName
             field.fieldType = "int"
         else:
             field = self.extractField(cellValue)
             if(not field):
                 print "Sheet::%s   Warnning --- Ignore column '%s'" % (currSheet.name,cellValue)
                 colIndex = colIndex + 1
                 continue
             
         anyFieldAlready = Util.getDictValue(dictColumns, field.fieldName)
         
         if(anyFieldAlready):
             raise Exception("Sheet %s The filed %s is already available kindly rename it to something else for this sheet col=(%d) " % (currSheet.name, field.fieldName,colIndex ))
         
         dictColumns[field.fieldName] = field.fieldType
         arrColumns.append(field)
             
             #print "field name is %s and field type is %s " % (field.fieldName,str(field.fieldType)) 
         colIndex = colIndex + 1
         
     mainDataDict = {}
     dataDict = mainDataDict[Rules.dataName] = {}
     
     for row_index in xrange(currSheet.nrows): 
         col = 0
         if(row_index==0):#skip the first row
             continue
         
         try:
             idKeyValue = long(float(Util.cellVal(currSheet, row_index, col))) # pick the 0th column value
         except Exception , e:
             raise Exception("Exception %s while working with Sheet ('%s')  Row_No=%i and Col=%i -------- Celll_Value =%s " % (str(e), currSheet.name,row_index, col, str(Util.cellVal(currSheet, row_index, col))))
             
         currRow = dataDict[str(idKeyValue)] = {}
         for column in arrColumns:
             cellValue = Util.cellVal(currSheet, row_index, col)
             try:
                 cellValue = Util.getCastedValue(cellValue,column.fieldType)
             except Exception , e:
                 print (str(e))
                 print "exception in sheet %s in row =%i col=%i " % (currSheet.name,row_index,col)
                 raise Exception("exception %s  %s sheet in row =%i col=%i " % (str(e), currSheet.name,row_index,col))
             
             if(cellValue):
                 currRow[column.fieldName] = cellValue 
             
             col = col + 1