使用XLRD从Excel列中获取最大值
我想使用xlrd从Excel电子表格的第25列中获得最大值。 这是我做的。
import xlrd book = xlrd.open_workbook("File Location\Filename.xlsx") sheet = book.sheet_by_index(0) def follow_up(): col = 24 row = 1 max = 1 while row < 100: a = sheet.cell(row, col).value if a>max: return a else: return max row+=1 print(follow_up())
我碰到一个问题的单元格中的值小于100的值(给我IndexError)和代码将不适用于一个列中有超过100个值的单元格。 如果我知道如何获得列中值的数量,这可以被修复。 但是我想知道是否有人知道一个“更干净”的方式来做到这一点。
非常感谢。
尝试:
import xlrd book = xlrd.open_workbook("File Location\Filename.xlsx") sheet = book.sheet_by_index(0) def follow_up(): col = 24 return max(sheet.col_values(col)) print(follow_up())
我希望这有帮助。
尝试这个:
import xlrd book = xlrd.open_workbook("File Location\Filename.xlsx") sheet = book.sheet_by_index(0) def follow_up(): col = 24 return max(sheet.col_values(col, start_rowx=1, end_rowx=101)) #with start_rowx and end_rowx you can define the range #we start with 1 so as to skip the header row print(follow_up())
col_values()
返回你提到的列中所有值的列表。
希望这可以帮助 :)