第222页中的示例错误及解决方法
>>>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
>>>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
© 本文版权归作者 近江大迫杰 所有,任何形式转载请联系作者。
有关键情节透露