简体   繁体   English

Python:使用 xlrd 库从 excel 电子表格中读取数据给了我不正确的行数

[英]Python: Using xlrd library to read data from excel spreadsheet gives me incorrect number of rows

I was using the xlrd library to read a workbook.我正在使用 xlrd 库来阅读工作簿。 I used the following code to read the columns and rows containing words and put it into a dictionary:我使用以下代码读取包含单词的列和行并将其放入字典中:

import xlrd

EXCEL_FILE = "Hangman_words.xlsx"

def main():
    """Main application entry point."""
    # To open Workbook 
    wb = xlrd.open_workbook(EXCEL_FILE) 
    sheet = wb.sheet_by_index(0) 
    
    word_dict = {}
    
    for i in range(sheet.ncols):
        try:
            #print(sheet.cell_value(0, i))
            category_name = sheet.cell_value(0, i)
            if not category_name:
                break
            word_dict[category_name] = []
            for j in range(sheet.nrows):
                try:
                    #print(sheet.cell_value(j+1, i))
                    word = sheet.cell_value(j+1, i)
                    if not word:
                        break
                    word_dict[category_name].append(word)
                except:
                    break
        except:
            break

    print(word_dict)

if __name__ == "__main__":
    main()

In my excel file, column 1 goes all the way to row 51, column 2 goes all the to row 53 and column 3 goes all the way to row 38. However, in my python code, the range(sheet.ncols) is set to (0,3) and range(sheet.nrows) is set to (0,53) and this row number is not correct for each column because as described previously, it varies.在我的 excel 文件中,第 1 列一直到第 51 行,第 2 列一直到第 53 行,第 3 列一直到第 38 行。但是,在我的 Python 代码中,设置了范围(sheet.ncols) to (0,3) 和 range(sheet.nrows) 设置为 (0,53) 并且此行号对于每列不正确,因为如前所述,它会有所不同。

在此处输入图片说明

My code also throws an exception for column 2. Hence I had to set up some exceptions and if conditions to ensure that the code will break out of the loop incase if there's any error or if there's a blank cell.我的代码也为第 2 列抛出异常。因此,我必须设置一些异常和 if 条件以确保代码在出现任何错误或存在空白单元格时跳出循环。

Since, I am pretty new to using this xlrd library, I was wondering if there is a way to get the correct number of rows for each column that I can iterate through?因为,我对使用这个 xlrd 库还很陌生,我想知道是否有办法为我可以迭代的每列获取正确的行数? Or if there's a better xl library that can outperform the current library that I am using.或者,如果有更好的 xl 库可以胜过我正在使用的当前库。 Thanks in advance.提前致谢。

"the range(sheet.ncols) is set to (0,3) and range(sheet.nrows) is set to (0,53) " --Correct. “范围(sheet.ncols)设置为(0,3),范围(sheet.nrows)设置为(0,53)”--正确。 xlrd would only limit the data range from 1stRow1stCol to 53rdRow3rdCol xlrd 只会限制从 1stRow1stCol 到 53rdRow3rdCol 的数据范围

What makes trouble is that you have null values at the bottom of your columns.麻烦的是您的列底部有空值。 You have to analyze it and stop it from further reading.你必须分析它并阻止它进一步阅读。

"My code also throws an exception for column 2" “我的代码也引发了第 2 列的异常”

            for j in range(sheet.nrows):
                try:
                    #print(sheet.cell_value(j+1, i))
                    word = sheet.cell_value(j+1, i)

as j comes to the last of range(53), j=52 sheet.cell_value(53, 0) Of course it breaches the data range in xlrd, since the nrow is limited to 0,1,...,52 A correction would be:当 j 来到 range(53) 的最后一个时, j=52 sheet.cell_value(53, 0) 当然它违反了 xlrd 中的数据范围,因为 nrow 被限制为 0,1,...,52 一个更正将是:

            for j in range(1, sheet.nrows):
                try:
                    #print(sheet.cell_value(j, i))
                    word = sheet.cell_value(j, i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM