How to check if value is None [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 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.

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.

Key Error even though key IS in dictionary? [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 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)

I want to print names of employees who have both work number and mobile number.below is my json body [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to print firstname of employees who have both work number and mobile number. Below is my json body. I am facing difficulty in getting inside phoneNumbers attribute. My final output should be: "Adhi as Adhi has both work and mobile numbers".
I am not able to iterate the inner dictionary of phoneNumbers attribute.Can you please help me on this.
This is my python code
for i in Data['users']:
for j in i['phoneNumbers']:
for i in range(len(j)):
if j['type']=="work" and j['type']=="mobile":
print("Firstname",i['firstName'])
You can loop over the users and check if the work and mobile number are present:
for user in Data['users']:
has_mobile_number = False
has_work_number = False
for phonenumber in user['phoneNumbers']:
if phonenumber['type'] == 'work':
has_work_number = True
if phonenumber['type'] == 'mobile':
has_mobile_number = True
if has_work_number and has_mobile_number:
print('Firstname', user['firstName'])
Also, I recommend not using i and j when not talking about indexes. In you code, i is a dict representing a user and j is a dict representing a phone. I replaced them with user and phonenumber for more clarity in the code above.

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

Categories