Python Forum
TypeError: 'NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'NoneType' object is not subscriptable
#1
Hi guys am new to python,

pls help trouble shoot this code i get the following error when executing the code in jupiter notebook



Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-23-19227ff4609b> in <module> 8 if len(line.strip()) == 0: 9 continue ---> 10 tweets.append(json.loads(line))['text'] 11 12 with open(labels_filename) as inf: TypeError: 'NoneType' object is not subscriptable
# load tweets and labels

tweets = []


with open(input_filename)as inf:
    for line in inf:
        if len(line.strip()) == 0:
            continue
        tweets.append(json.loads(line))['text']

with open(labels_filename) as inf:
        labels = json.load(inf)
        
tweets = tweets[:len(labels)]
Reply
#2
Please post error and code within python tags.

This error simply states wherever you are storing data or returning data is empty or NONE.


Secondly here:

with open(labels_filename) as inf:
    labels = json.load(inf)# you loading object directly to json which is file object and not using loop something like below

    tweets = tweets[:len(labels)]
with open(labels_filename) as inf:
    ** loop ** here

    labels = json.load(inf)

    tweets = tweets[:len(labels)]
Reply
#3
# load tweets and labels

tweets = []


with open(input_filename)as inf:
    for line in inf:
        if len(line.strip()) == 0:
            continue
        tweets.append(json.loads(line))['text']

with open(labels_filename) as inf:
        labels = json.load(inf)
        
tweets = tweets[:len(labels)]
Error:
TypeError Traceback (most recent call last) <ipython-input-23-19227ff4609b> in <module> 8 if len(line.strip()) == 0: 9 continue ---> 10 tweets.append(json.loads(line))['text'] 11 12 with open(labels_filename) as inf: TypeError: 'NoneType' object is not subscriptable
Reply
#4
I have edited my answer, please check
Reply
#5
the labels are in a file stored in below location: so the code is supposed to load from labels_filename
 # input file
 input_filename = os.path.join(os.path.expanduser("~"),"data_folder", "bonny.json")
 # output file
labels_filename = os.path.join(os.path.expanduser("~"), "data_folder","bonny_labels.json")
[/icode]

full code to load tweets with labels is as below:

[icode]tweets = []
with open(input_filename) as inf:
    for line in inf:
        if len(line.strip()) == 0: continue
        tweets.append(json.loads(line)['text'])

with open(labels_filename) as inf:
    labels = json.load(inf)

# Ensure only classified tweets are loaded
tweets = tweets[:len(labels)]
]
Reply
#6
In general, the error means that you attempted to index an object that doesn't have that functionality. You are trying to subscript an object which you think is a list or dict, but actually is None. NoneType is the type of the None object which represents a lack of value, for example, a function that does not explicitly return a value will return None. 'NoneType' object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the __getitem__ method .
Reply
#7
The error is self-explanatory. You are trying to subscript an object which is a None actually...

Example 1

list1=[5,1,2,6]   # Create a simple list
order=list1.sort()  # sort the elements in created list and store into another variable.
order[0]  # Trying to access the first element after sorting

TypeError     Traceback (most recent call last)

in ()
  list1=[5,1,2,6]
  order=list1.sort()
----> order[0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 273 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 922 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 3,841 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Help with python 'not subscriptable' error Extra 3 1,980 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,375 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov

Forum Jump:

User Panel Messages

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