【问题标题】:TypeError: 'int' object is not subscriptable using xlrdTypeError: 'int' 对象不能使用 xlrd 下标
【发布时间】:2020-09-28 10:50:14
【问题描述】:

我想使用 xlrd 从 excel 表中读取数据。我想通过 excel 中的特定单元格,如果我没有找到数字“1”,我想在行号上添加 +1 并重新开始。我的问题是,当我在 for 循环中指定单元格时,我得到一个 TypeError:'int' object is not subscriptable。

import xlrd

Excelsheet1 = "info_2020.xlsx" 
Book1 = xlrd.open_workbook(Excelsheet1) 
first_sheet = Book1.sheet_by_index(9)

row_num = 1

for look_for_one in first_sheet.row_values(row_num[12]):
        if look_for_one == 1:
            print(first_sheet.row_values(row_num)[25])
        else:
            row_num += 1

有谁知道怎么回事?谢谢!

【问题讨论】:

  • 您似乎忘记了括号并写了first_sheet.row_values(row_num[12])而不是first_sheet.row_values(row_num)[12]。因此错误消息是“int is not subscriptable”,意思是“我不明白row_num[12],因为row_num 是一个int(数字),而不是一个列表。”
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
  • 如果错误消息显示哪一行有问题,然后使用print(...)print(type(...)) 查看变量中的内容。
  • 谢谢 Stef,你是对的。现在它正在工作!
  • @Stef 对我来说,这将是一个有效的答案。

标签: python excel xlrd


【解决方案1】:

您似乎忘记了括号并写了first_sheet.row_values(row_num[12])而不是first_sheet.row_values(row_num)[12]

因此错误消息是“int is not subscriptable”,意思是“我不明白row_num[12],因为row_num 是一个int(数字),而不是一个列表。”

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 2012-02-21
    • 2018-08-07
    • 2023-04-02
    • 2017-11-10
    • 2023-03-20
    • 2015-07-31
    相关资源
    最近更新 更多