第222页中的示例错误及解决方法

xiao爱
>>>import openpyxl
>>>wb = openpyxl.load_workbook('example.xlsx')
>>>sheet = wb.get_active_sheet()
>>>sheet.columns[1]
上面的示例为运行后,会产生如下报错:
TypeError: 'generator' object is not subscriptable
用以下的方式访问即可
>>> list(sheet.columns)[0]
(<Cell Sheet1.A1>, <Cell Sheet1.A2>, <Cell Sheet1.A3>, <Cell Sheet1.A4>, <Cell Sheet1.A5>, <Cell Sheet1.A6>, <Cell Sheet1.A7>)
>>> list(sheet.columns)[1]
(<Cell Sheet1.B1>, <Cell Sheet1.B2>, <Cell Sheet1.B3>, <Cell Sheet1.B4>, <Cell Sheet1.B5>, <Cell Sheet1.B6>, <Cell Sheet1.B7>)
>>> sheet["A"]
(<Cell Sheet1.A1>, <Cell Sheet1.A2>, <Cell Sheet1.A3>, <Cell Sheet1.A4>, <Cell Sheet1.A5>, <Cell Sheet1.A6>, <Cell Sheet1.A7>)

答案来源:
显示全文
>>>import openpyxl
>>>wb = openpyxl.load_workbook('example.xlsx')
>>>sheet = wb.get_active_sheet()
>>>sheet.columns[1]
上面的示例为运行后,会产生如下报错:
TypeError: 'generator' object is not subscriptable
用以下的方式访问即可
>>> list(sheet.columns)[0]
(<Cell Sheet1.A1>, <Cell Sheet1.A2>, <Cell Sheet1.A3>, <Cell Sheet1.A4>, <Cell Sheet1.A5>, <Cell Sheet1.A6>, <Cell Sheet1.A7>)
>>> list(sheet.columns)[1]
(<Cell Sheet1.B1>, <Cell Sheet1.B2>, <Cell Sheet1.B3>, <Cell Sheet1.B4>, <Cell Sheet1.B5>, <Cell Sheet1.B6>, <Cell Sheet1.B7>)
>>> sheet["A"]
(<Cell Sheet1.A1>, <Cell Sheet1.A2>, <Cell Sheet1.A3>, <Cell Sheet1.A4>, <Cell Sheet1.A5>, <Cell Sheet1.A6>, <Cell Sheet1.A7>)

答案来源:https://stackoverflow.com/questions/42603795/typeerror-generator-object-is-not-subscriptable
0
0

查看更多豆瓣高分好书

回应(0)

添加回应

Python编程快速上手的更多书评

推荐Python编程快速上手的豆列

了解更多图书信息

值得一读

    豆瓣
    我们的精神角落
    免费下载 iOS / Android 版客户端