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)]
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)]
# 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
I have edited my answer, please check
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)]
]
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 .
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]