Python Forum
object is not subscriptable... Error Message?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
object is not subscriptable... Error Message?
#1
I am trying to access values of cells in an exel workbook. i am following an exercise from a book and getting the following error message;
>>> import openpyxl
>>> wb=openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet=wb.active
>>> sheet.columns[1]
Traceback (most recent call last):
  File "<pyshell#151>", line 1, in <module>
    sheet.columns[1]
TypeError: 'generator' object is not subscriptable
any idea what i am doing wrong?
Reply
#2
'Worksheet.columns' is a generator, you cannot subscript it (i.e. use square bracket notation to access elements)
you can iterate over it, or convert it to list/tuple and then use subscript.
https://openpyxl.readthedocs.io/en/stabl...many-cells

https://openpyxl.readthedocs.io/en/stabl...et.columns
Again it is change in API in 2016
https://openpyxl.readthedocs.io/en/stabl...2016-04-11
ws.rows and ws.columns now always return generators and start at the top of the worksheet
Maybe you should find more recent book or carefully check changelog for discrepancies
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
thank you for the link. I have reviewed and am unable to find the details i am looking for. I am following an exercise in a book called Automate the boring stuff. i am following the exercise exactly and typing the following code
>>> import openpyxl
>>> wb=openpyxl.load_workbook('Example_Page267.xlsx')
>>> sheet=wb.active
>>> sheet.columns[1]

the output the book is saying i should get is
(<cell Sheet1.B1>,<cell Sheet1.B2>,<cell Sheet1.B3>,<cell Sheet1.B4>,<cell Sheet1.B5>,<cell Sheet1.B4>,<cell Sheet1.B5>,<cell Sheet1.B6>,<cell Sheet1.B7>)
but instead i am getting the error message
Traceback (most recent call last):
  File "<pyshell#160>", line 1, in <module>
    sheet.columns[1]
TypeError: 'generator' object is not subscriptable
could you give me an example of the code i should be inputting to get that output?
Reply
#4
In the first link I provided it shows you can convert it to tuple and display all of it. In your snippet you want to get just the element with index 1.
>>>tuple(sheet.columns)[1]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 678 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  Error message about iid from RandomizedSearchCV Visiting 2 932 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Need help with 'str' object is not callable error. Fare 4 775 Jul-23-2023, 02:25 PM
Last Post: Fare
  Another Error message. the_jl_zone 2 942 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Help with python 'not subscriptable' error Extra 3 1,976 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,161 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  Error in Int object is not subscript-able. kakut 2 1,131 Jul-06-2022, 08:31 AM
Last Post: ibreeden
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,673 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,854 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  'int' object is not subscriptable after API call ed8484 1 1,765 Sep-18-2021, 02:06 PM
Last Post: ed8484

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020