def test_xlrd(self): WayPointsDict = {} ColumnNames = [] fileName = 'WayPoints.xls' if ('FlightDynamics' in os.getcwd()) and not('Home' in os.getcwd()): fileName = os.getcwd() + os.path.sep + 'Home' + os.path.sep + 'xlrd' + os.path.sep + 'tests' + os.path.sep + fileName else: ''' case when the run is launched from Home ''' fileName = os.getcwd() + os.path.sep + fileName print 'file name= {0}'.format(fileName) book = open_workbook(fileName) sheet = book.sheet_by_index(0) for row in range(sheet.nrows): rowValues = sheet.row_values(row, start_colx=0, end_colx=sheet.ncols) # Print the values of the row formatted to 10 characters wide if row == 0: ColumnNames = list(rowValues) print ColumnNames else: if rowValues[0] in WayPointsDict.keys(): raise ValueError ('key- {0} already existing'.format(rowValues[0])) WayPointsDict[rowValues[0]] = rowValues print tuple(rowValues) print '===================' print ColumnNames print '===================' self.assertTrue(len(WayPointsDict)>0) for key in WayPointsDict.keys(): print key, WayPointsDict[key]
def read(self): assert len(self.FilePath) > 0 self.book = open_workbook(self.FilePath) ''' assert there is only one sheet ''' sheet = self.book.sheet_by_name('WayPoints') for row in range(sheet.nrows): rowValues = sheet.row_values(row, start_colx=0, end_colx=sheet.ncols) # Print the values of the row formatted to 10 characters wide if row == 0: self.ColumnNames = {} index = 0 for column in rowValues: if column not in fieldNames: print self.className + ': ERROR - expected waypoints column name= {0} not in field names'.format( column) return False else: self.ColumnNames[column] = index index += 1 else: WayPointName = str(rowValues[0]).strip().upper() if not (WayPointName in self.WayPointsDict.keys()): wayPointDict = {} for column in self.ColumnNames: if column == 'Latitude' or column == 'Longitude': ''' replace degree character ''' strLatLong = unicode( rowValues[self.ColumnNames[column]]).strip() if u'°' in strLatLong: strLatLong = unicode(strLatLong).replace( u'°', '-') strLatLong = strLatLong.encode( 'ascii', 'ignore') strLatLong = str(strLatLong).strip().replace( u"'", u'-').replace(u' ', u'').replace(u'"', u'') #print 'lat-long= '+ strLatLong wayPointDict[ column] = convertDegreeMinuteSecondToDecimal( strLatLong) else: wayPointDict[column] = str( rowValues[self.ColumnNames[column]]).strip() ''' create a way point ''' wayPoint = WayPoint(wayPointDict['WayPoint'], wayPointDict['Latitude'], wayPointDict['Longitude']) self.WayPointsDict[WayPointName] = wayPoint return True
def read(self): assert len(self.FilePath)>0 self.book = open_workbook(self.FilePath) ''' assert there is only one sheet ''' sheet = self.book.sheet_by_name('WayPoints') for row in range(sheet.nrows): rowValues = sheet.row_values(row, start_colx=0, end_colx=sheet.ncols) # Print the values of the row formatted to 10 characters wide if row == 0: self.ColumnNames = {} index = 0 for column in rowValues: if column not in fieldNames: print self.className + ': ERROR - expected waypoints column name= {0} not in field names'.format(column) return False else: self.ColumnNames[column] = index index += 1 else: WayPointName = str(rowValues[0]).strip().upper() if not(WayPointName in self.WayPointsDict.keys()): wayPointDict = {} for column in self.ColumnNames: if column == 'Latitude' or column == 'Longitude': ''' replace degree character ''' strLatLong = unicode(rowValues[self.ColumnNames[column]]).strip() if u'°' in strLatLong: strLatLong = unicode(strLatLong).replace(u'°','-') strLatLong = strLatLong.encode('ascii', 'ignore') strLatLong = str(strLatLong).strip().replace(u"'", u'-').replace(u' ',u'').replace(u'"',u'') #print 'lat-long= '+ strLatLong wayPointDict[column] = convertDegreeMinuteSecondToDecimal(strLatLong) else: wayPointDict[column] = str(rowValues[self.ColumnNames[column]]).strip() ''' create a way point ''' wayPoint = WayPoint(wayPointDict['WayPoint'], wayPointDict['Latitude'], wayPointDict['Longitude']) self.WayPointsDict[WayPointName] = wayPoint return True
def read(self): ''' this method does not read the whole file - only the headers ''' assert len(self.FilePath)>0 and os.path.isfile(self.FilePath) book = open_workbook(self.FilePath, formatting_info=True) ''' assert there is only one sheet ''' self.sheet = book.sheet_by_index(0) for row in range(self.sheet.nrows): rowValues = self.sheet.row_values(row, start_colx=0, end_colx=self.sheet.ncols) if row == 0: self.ColumnNames = {} index = 0 for column in rowValues: if column not in fieldNames: print self.className + ': ERROR - expected runway column name= {0} not in field names'.format(column) return False else: self.ColumnNames[column] = index index += 1 break return True
def test_xlrd(self): WayPointsDict = {} ColumnNames = [] fileName = 'WayPoints.xls' if ('FlightDynamics' in os.getcwd()) and not ('Home' in os.getcwd()): fileName = os.getcwd( ) + os.path.sep + 'Home' + os.path.sep + 'xlrd' + os.path.sep + 'tests' + os.path.sep + fileName else: ''' case when the run is launched from Home ''' fileName = os.getcwd() + os.path.sep + fileName print 'file name= {0}'.format(fileName) book = open_workbook(fileName) sheet = book.sheet_by_index(0) for row in range(sheet.nrows): rowValues = sheet.row_values(row, start_colx=0, end_colx=sheet.ncols) # Print the values of the row formatted to 10 characters wide if row == 0: ColumnNames = list(rowValues) print ColumnNames else: if rowValues[0] in WayPointsDict.keys(): raise ValueError('key- {0} already existing'.format( rowValues[0])) WayPointsDict[rowValues[0]] = rowValues print tuple(rowValues) print '===================' print ColumnNames print '===================' self.assertTrue(len(WayPointsDict) > 0) for key in WayPointsDict.keys(): print key, WayPointsDict[key]
[Direct access through book.name_and_scope_map] Revenue -1 0 checks if "Revenue" exists in global scope """ sys.stdout.write(text) if len(sys.argv) != 5: usage() sys.exit(0) arg_pattern = sys.argv[1] # glob pattern e.g. "foo*.xls" arg_name = sys.argv[2] # see below arg_scope = sys.argv[3] # see below arg_show_contents = int( sys.argv[4]) # 0: no show, 1: only non-empty cells, # 2: all cells for fname in glob.glob(arg_pattern): book = open_workbook(fname) if arg_name == "*": # Examine book.name_obj_list to find all names # in a given scope ("*" => all scopes) do_scope_query(book, arg_scope, arg_show_contents) elif arg_scope == "*": # Using book.name_map to find all usage of a name. show_name_details(book, arg_name, arg_show_contents) else: # Using book.name_and_scope_map to find which if any instances # of a name are visible in the given scope, which can be supplied # as -1 (global) or a sheet number or a sheet name. show_name_details_in_scope(book, arg_name, arg_scope, arg_show_contents)
[Initial direct access through book.name_map] Sales * 0 lists all occurrences of "Sales" in any scope [Direct access through book.name_and_scope_map] Revenue -1 0 checks if "Revenue" exists in global scope """ sys.stdout.write(text) if len(sys.argv) != 5: usage() sys.exit(0) arg_pattern = sys.argv[1] # glob pattern e.g. "foo*.xls" arg_name = sys.argv[2] # see below arg_scope = sys.argv[3] # see below arg_show_contents = int(sys.argv[4]) # 0: no show, 1: only non-empty cells, # 2: all cells for fname in glob.glob(arg_pattern): book = open_workbook(fname) if arg_name == "*": # Examine book.name_obj_list to find all names # in a given scope ("*" => all scopes) do_scope_query(book, arg_scope, arg_show_contents) elif arg_scope == "*": # Using book.name_map to find all usage of a name. show_name_details(book, arg_name, arg_show_contents) else: # Using book.name_and_scope_map to find which if any instances # of a name are visible in the given scope, which can be supplied # as -1 (global) or a sheet number or a sheet name. show_name_details_in_scope(book, arg_name, arg_scope, arg_show_contents)