我想读取一个范围内的行数。我有一个包含很多行的文件。我想知道从A列到D列的列范围内的行数。
workbook = xlrd.open_workbook(file)
sheet = workbook.sheet_by_index(0)
sheet.nrows[0:4] #I want to know the number of rows within that range, or "A{}:D{}"
[如果有人知道执行此操作的方法,请帮助我。谢谢!
您可以遍历各列...使用循环:
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
使用xlrd并没有任何问题,这也是我首先使用的。但是,我建议使用pandas库,因为它在整个python社区中使用更多。
import pandas
dataframe = pandas.read_excel(file, usecols='A:D')
print(len(dataframe))