Key Error even though key IS in dictionary? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Dictionary:
section of dictionary
My Code:
code
Error says:
3
So how come the date key works fine but for freq it fails?
ps. my first time posting, so am very sorry for the sloppy structure of the post

This can only happen when one of your day is missing the freq parameter.
Try catching the day in which the error is happening. Then manually check that particular entry.
date_list = []
frequency_list = []
try:
for i in obj:
date = obj[i]["date"]
frequency = obj[i]["freq"]
date_list.append(date)
frequency_list.append(frequency)
except:
print(i)

Related

Access sub category in JSON python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
What is the code required to print the ContractName sub category (under result) ?
I know to get result I have my dictionary result :
response_dict = response.json()
print()response_dict["result"]
But how do I get ContractName??
{
"status":"1",
"message":"OK",
"result":[
{
"SourceCode":"test",
"ContractName":"DAO",
"CompilerVersion":"v0.3.1-2016-04-12-3ad5e82",
}
]
}
The value of result in your dictionary is a list. That list, in your example, contains one element which is another dictionary.
Therefore:
response_dict['result'][0]['ContractName']
...will give you what you need.

How to check if value is None [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to check if the "nilai" is None/Null
cursor.execute('SELECT s.kode_ktg_id,(SUM(n.nilai_angka * S.nilai_mk)/SUM(s.nilai_mk)) as nilai_ktg FROM mahasiswa_khs k, mahasiswa_convert_nilai n, mata_kuliah_si s WHERE k.nilai = n.nilai_huruf AND k.nim = "%s" AND k.kode = s.kode GROUP BY s.kode_ktg_id',[nim])
nilai = cursor.fetchall()
I check with this
if nilai[0] is None:
But I got error tuple index out of range
This is because nilai is an empty tuple, since it returned no records.
You can check if it is empty with:
if not nilai:
# no records
else:
# at least one record
That being said, in Django you can make use of the Django ORM, which is often safer, and wraps elements in model objects.

list index out of range when upgrating python/django [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
fa = Fa.objects.filter(fa_name = tag)[0]
It was working in python 2.7 and django 1.8 but now that I migrated to django 2.2 and python 3.6 its not working
If you want to get the first if no data then None you should use first method:
fa = Fa.objects.filter(fa_name = tag).first()
It will return you None if you have no data and if you do have then it will return the first element
If you want to avoid any None values then you should check before executing it:
if Fa.objects.filter(fa_name = tag).count() > 0:
fa = fa = Fa.objects.filter(fa_name = tag)[0]

How to find an item in a dict with nested classes? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm a newbie programming in python and I canĀ“t find a element in a complex dict (for me at least).
This dict contains items "FareAttribute" and the same time this class contains elements "FareRule". I want to find the element that matches FareRule.origin_id=="city1" and FareRule.destination_id=="city2".
How I can to find this?
Thanks for any comment in advance. I'm a bit lost
Edit to add dict (output when print first item). The classes belongs to transitfeed library (Google Transit). Right now I can't execute program, I'm out.
{u'AA': <FareAttribute [('currency_type', u'EUR'), ('fare_id', u'AA'), ('payment_method', 0), ('price', 1.5), ('rules', [<FareRule [('contains_id', None), ('destination_id', u'A'), ('fare_id', u'AA'), ('origin_id', u'A'), ('route_id', None)]>]), ('transfer_duration', None), ('transfers', 0)]>,...}
EDIT2 Please try something like this (if python 2.7):
for fare in schedule.GetFareAttributeList():
for rule in fare.GetFareRuleList():
if rule.origin_id == 'B1' and rule.destination_id == 'B1':
print rule

Python generator expression Twitter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am kinda new to working with json and python and I am stuck on the parsing the data in js to generate an expression. I would really appreciate anyone suggestions on the best path to take.
Here is the Data I am working with
{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Fri Dec 06 15:06:44 +0000 2013","id":408975801577926656,"id_str":"408975801577926656","text":"RT #jk636575: #GhanaNudes contact me if you want to swing\njk636575#gmail.com","user":{"id":974873810,"id_str":"974873810","name":"Gh Nudes","screen_name":"GhanaNudes","location"
Here is my code:
def main():
ts = TwitterSearch()
response, data = ts.search('#gmail.com', result_type='recent')
js = json.loads(data)
messages = ([data_items] for msg in js)
I need to parse the content in js and turn it into a generator expression so that I only write: Created_at , text , user:{is
Based on the Twitter search API docs,
messages = ([msg['created_at'], msg['txt'], msg['user']['id']] for msg in js['statuses'])
Note that I have updated my answer to the original question to include this.
Edit: It might be a bit safer to replace in js['statuses'] with in js.get('statuses', []).

Categories