【问题标题】:Read the number of rows in a range with xlrd - Python使用 xlrd - Python 读取范围内的行数
【发布时间】:2020-01-15 04:09:18
【问题描述】:

我想读取一个范围内的行数。我有一个包含很多行的文件。我想知道列范围内的行数,比如说从 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{}"

如果有人知道这样做的方法,请帮助我。谢谢!

【问题讨论】:

    标签: python xlrd


    【解决方案1】:

    您可以遍历列... 使用循环:

    for col_idx in range(0, num_cols): # 遍历列 cell_obj = xl_sheet.cell(row_idx, col_idx) # 逐行获取单元格对象, col

    【讨论】:

      【解决方案2】:

      使用 xlrd 并没有什么问题,这也是我最先使用的。不过,我建议使用 pandas 库,因为它在整个 python 社区中使用得更多。

      import pandas
      
      dataframe = pandas.read_excel(file, usecols='A:D')
      print(len(dataframe))
      

      【讨论】:

      • 谢谢!不知道。对于数据操作,语法看起来更好更容易。
      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2020-09-15
      • 2023-03-03
      相关资源
      最近更新 更多