KeyError in Python, even though the key exists - python

I have been scratching my head on this for a few days now and cannot seem to find a solution that works online for my problem. I am trying to access data on zendesk and go through the pagination. For some reason, I am getting a KeyError, even though I can see that the key does exist. Here is my code :
data_users2 = [[]]
while url_users:
users_pagination = requests.get(url_users,auth=(user, pwd))
data_user_page = json.loads(users_pagination.text)
print (data_user_page.keys())
for user in data_user_page['users']:
data_users2.append(user)
url = data_user_page['next_page']
Here is the output :
dict_keys(['users', 'next_page', 'previous_page', 'count'])
dict_keys(['error'])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-fab95d95ddeb> in <module>
6 data_user_page = json.loads(users_pagination.text)
7 print (data_user_page.keys())
----> 8 for user in data_user_page["users"]:
9 data_users2.append(user)
10 url = data_user_page["next_page"]
KeyError: 'users'
As you can see, users does exist. same thing happens if I try to print the next_page, I get a KeyError for next_page.
Any help would be appreciated ! Thanks!

Your code is failing in its second iteration of the loop, in that moment your keys in data_user_page are just "error" as you can see in the output you have pasted
dict_keys(['users', 'next_page', 'previous_page', 'count']) <----- FIRST ITERATION
dict_keys(['error']) <---- SECOND ITERATION, THEREFORE, YOUR KEY DOES NOT EXISTS
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-fab95d95ddeb> in <module>
6 data_user_page = json.loads(users_pagination.text)
7 print (data_user_page.keys())
----> 8 for user in data_user_page["users"]:
9 data_users2.append(user)
10 url = data_user_page["next_page"]
KeyError: 'users'
EDIT: This could be due to the fact that you are saving the next url in a variable called url not url_users

Related

I am getting ValueError: list.remove(x): x not in list on my code

code_arr=[]
for i in word_tokenize(student_code):
code_arr.append(i)
print(code_arr)
print(len(code_arr))
codet_arr=[]
for i in word_tokenize(teacher_code):
codet_arr.append(i)
print(codet_arr)
print(len(codet_arr))
for code_s in code_arr:
for code_t in codet_arr:
if code_s==code_t:
code_arr.remove(code_t)
else:
continue
The above code gives below error:
ValueError Traceback (most recent call last)
<ipython-input-13-4fe8ea0c7232> in <module>()
2 for code_t in codet_arr:
3 if code_s==code_t:
----> 4 code_arr.remove(code_t)
5 else:
6 continue
ValueError: list.remove(x): x not in list
Without knowing the content of the arrays it's difficult to duplicate but I suspect your problem is in modifying the content of an array that you're iterating over.
For clarity, I tend to build a set of values that I want to remove, then remove them in a separate loop, viz...
to_remove = set()
for code_s in code_arr:
for code_t in codet_arr:
if code_s == code_t:
to_remove.add(code_t)
for code in to_remove:
code_arr.remove(code)
Debugging is easier too as you can check the codes to be removed before removing them.
Ps. You don't need the 'else continue' on the end of a loop.
I think this will work :
for i in code_arr:
while i in code_t:
code_t.remove(i)
print(code_t)

Getting ValueError after last iteration when using .apply() method

I wrote a function to query emotion analysis from senpy by passing reviews. I printed every line with index to see if it just worked fine. The dataset has 5684 rows. However, when reaching the last row I get a ValueError. I also tried to add this last review to my function and I received the corresponding values successfully.
This is the function I wrote.
def query_emotion(review):
params = {'input': review}
res = requests.get('http://senpy.gsi.upm.es/api/emotion-depechemood',
params=params)
if res.status_code != 200:
raise Exception(res)
data = json.loads(res.text)
negative_fear = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][0]['onyx:hasEmotionIntensity']
amusement = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][1]['onyx:hasEmotionIntensity']
anger = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][2]['onyx:hasEmotionIntensity']
annoyance = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][3]['onyx:hasEmotionIntensity']
indifference = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][4]['onyx:hasEmotionIntensity']
joy = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][5]['onyx:hasEmotionIntensity']
awe = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][6]['onyx:hasEmotionIntensity']
sadness = data['entries'][0]['onyx:hasEmotionSet'][0]['onyx:hasEmotion'][7]['onyx:hasEmotionIntensity']
print(X[X.Text == review].index, ": ", [negative_fear, amusement, anger, annoyance, indifference, joy, awe, sadness])
return negative_fear, amusement, anger, annoyance, indifference, joy, awe, sadness
I called the function using the .apply() method:
X['Text_negative_fear'], X['Text_amusement'], X['Text_anger'], X['Text_annoyance'], X['Text_indifference'], X['Text_joy'], X['Text_awe'], X['Text_sadness'] = X.Text.apply(query_emotion)
And these are the last lines from the output after calling the function:
Int64Index([5681], dtype='int64') : [0.0792444511959933, 0.1580643288473154, 0.11423923859401869, 0.1399028217635615, 0.13737889283844476, 0.10330318175060896, 0.1746433112249919, 0.09322377378506547]
Int64Index([5682], dtype='int64') : [0.08308025773764179, 0.1820866455048511, 0.09436993092693748, 0.12984061502089508, 0.1281518690206751, 0.10726563771574184, 0.19287900349802356, 0.08232604057523404]
Int64Index([5683], dtype='int64') : [0.09470651839679665, 0.19571514056396988, 0.10608728359324908, 0.12185687329212973, 0.12744650875201016, 0.10307696316708366, 0.150327288948556, 0.10078342328620486]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-6f8cc4431e17> in <module>()
----> 1 X['Text_negative_fear'], X['Text_amusement'], X['Text_anger'], X['Text_annoyance'], X['Text_indifference'], X['Text_joy'], X['Text_awe'], X['Text_sadness'] = X.Text.apply(query_emotion)
ValueError: too many values to unpack (expected 8)
Thank you for any advice that might fix this issue!

How to get a list of all tokens from Lucene 8.6.1 index using PyLucene?

I have got some direction from this question. I first make the index like below.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator
index_path = "./index"
lucene.initVM()
analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)
doc = Document()
doc.add(Field("docid", "1", TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)
writer.close()
store.close()
I then try to get all the terms in the index for one field like below.
store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)
Attempt 1: trying to use the next() as used in this question which seems to be a method of BytesRefIterator implemented by TermsEnum.
for lrc in reader.leaves():
terms = lrc.reader().terms('title')
terms_enum = terms.iterator()
while terms_enum.next():
term = terms_enum.term()
print(term.utf8ToString())
However, I can't seem to be able to access that next() method.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while terms_enum.next():
5 term = terms_enum.term()
6 print(term.utf8ToString())
AttributeError: 'TermsEnum' object has no attribute 'next'
Attempt 2: trying to change the while loop as suggested in the comments of this question.
while next(terms_enum):
term = terms_enum.term()
print(term.utf8ToString())
However, it seems TermsEnum is not understood to be an iterator by Python.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while next(terms_enum):
5 term = terms_enum.term()
6 print(term.utf8ToString())
TypeError: 'TermsEnum' object is not an iterator
I am aware that my question can be answered as suggested in this question. Then I guess my question really is, how do I get all the terms in TermsEnum?
I found that the below works from here and from test_FieldEnumeration() in the test_Pylucene.py file which is in pylucene-8.6.1/test3/.
for term in BytesRefIterator.cast_(terms_enum):
print(term.utf8ToString())
Happy to accept an answer that has more explanation than this.

name 'FillMissing' is not defined

When I run following code:
df = pd.read_csv('../input/marketingrar/marketing.csv')
df.head()
dep_var = 'Revenue'
cat_names = ['Day_Name','Promo']
cont_names = ['Date','Week','Month','Month_ID','Year','Visitors','Marketing Spend']
procs = [FillMissing, Categorify, Normalize]
I got this error bellow:
NameError Traceback (most recent call
last) in
----> 1 procs = [FillMissing, Categorify, Normalize]
NameError: name 'FillMissing' is not defined
P.S. I'm using Kaggle notebook. Why this error occurs and how to solve it?
from fastai.tabular.all import *
is the only working solution for me
With this code, you are trying to initiate a list named procs with the 3 references to FillMissing, Categorify and Normalise, but you never created those references before.
Did you maybe want to create a list of 3 strings? Then you forgot the '', compare the other lists like cat_names or cont_names
Maybe it could also help to include
from fastai import *
from fastai.tabular import *

Python Error :'numpy.float64' object is not callable

I have written a code in python to generate a sequence of ARIMA model's and determine their AIC values to compare them.The code is as below,
p=0
q=0
d=0
for p in range(5):
for d in range(1):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
print(arima_mod.params)
print arima_mod.aic()
I am getting a error message as below,
TypeError Traceback (most recent call last)
<ipython-input-60-b662b0c42796> in <module>()
8 arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
9 print(arima_mod.params)
---> 10 print arima_mod.aic()
global arima_mod.aic = 1262.2449736558815
11
**TypeError: 'numpy.float64' object is not callable**
Remove the brackets after print arima_mod.aic(). As I read it, arima_mod.aic is 1262.2449736558815, and thus a float. The brackets make python think it is a function, and tries to call it. You do not want that (because it breaks), you just want that value. So remove the brackets, and you'll be fine.

Categories