I'm using ConfigParser which returns a dictionary of configuration data as such:
{'general': {'UserKey': 'thisisatestkey'}}
If I want to simply print the value of the UserKey key (in this case thisisatestkey), then I generally just do a print "Your key is: {0}".format(mydictvar.get('UserKey')).
If I just print out the raw dict to a string I get the above. If I use the print statement above I get result of None since there is no key in the root of the dict called UserKey. If I .get('general') I just get: {'UserKey': 'thisisatestkey'}
Obviously I could do a fore loop like so:
keydic = cp.get_config_data()
for m, k in keydic.iteritems():
for s, v in k.iteritems():
userkey = v
and then print userkey which works fine. But I want to know how I can just avoid having to do the entire for loop first and just print the darned value right inline? Thanks!
You can use
mydictvar['general']['UserKey']
Or, if keys might be missing
mydictvar.get('general', {}).get('UserKey')
mydictvar['general'] returns a dictionary object; you can then just apply [...] to that value to retrieve the next key.
This works in string formatting too:
>>> mydictvar = {'general': {'UserKey': 'thisisatestkey'}}
>>> print "Your key is: {0[general][UserKey]}".format(mydictvar)
Your key is: thisisatestkey
simply without loop:
>>> my_dict = {'general': {'UserKey': 'thisisatestkey'}}
>>> my_dict['general']['UserKey']
'thisisatestkey'
Related
working on Python script. I get a result that is list:
a = [{'S_RAF': {'C_C106': {'D_1103': 'AVE', 'D_1104': '3-AB3242'}}}, {'S_RAF': {'C_C106': {'D_1103': 'OI', 'D_1104': '31503302130'}}}, {'S_RAF': {'C_C106': {'D_1103': 'PQ', 'D_1104': 'IBAN3102495934895'}}}]
And I would like to get the value of Key: D_1104, when the value for key D_1103 is PQ.
what would be best way in python to get the value of this key in element S_RAF/C_C106/{D_1103=PQ}. function should return: IBAN3102495934895.
Thanks
I tried:
a[2]['C_C106']['D_1104']
but is not correct.
Should do it:
a[2]['S_RAF']['C_C106']['D_1104'] # IBAN3102495934895
You can iterate through the list and check the values in each dictionary like this:
for dictionary in a:
if dictionary['S_RAF']['C_C106']['D_1103'] == 'PQ':
iban = dictionary['S_RAF']['C_C106']['D_1104']
Get ISBNs where D_1103 == "PQ".
ibans = [x["S_RAF"]["C_C106"]["D_1104"] for x in a if x["S_RAF"]["C_C106"]["D_1103"]=="PQ"]
ibans = ibans[0] # "IBAN3102495934895"
I got a nested dictionary as follows and it will return the third key while inputting the first key in the dictionary
tree = {"Luke" : {"Darth Vader" : {"The Chancellor"}},
"Neal" : {"Les" : {"Joseph"}},
"George" : {"Fred" : {"Mickey"}},
"Robert" : {"Tim" : {"Michael"}},
"Juan" : {"Hunter" : {"Thompson"}}}
check_con = input("Enter your Name")
for fi_name,fi_second in tree.items():
if check_con in fi_name:
for fi_third,fi_fourth in fi_second.items():
print(fi_fourth)
I feel that its bit more steps, is there any other way to do it?
Regard
You can use dict.get method with a default value of empty dict to get the top level dict and then convert its values to a iter, use next to get the first value
>>> check_con = 'Neal'
>>> next(iter(tree.get(check_con, {}).values()), '')
{'Joseph'}
>>>
>>> check_con = 'xxx'
>>> next(iter(tree.get(check_con, {}).values()), '')
''
>>>
You can simply use a try-excep expression in order to find out whether your name exist in the dictionary or not. If it exist there you can then return all the values of the respective value:
def get_nested_item(tree, check_on):
try:
sub_dict = tree[check_on]
except KeyError:
print("No result")
return
else:
return sub_dict.values()
Also note that about checking the existence of your name in dictionary what you're ding here is a membership checking at following line:
if check_con in fi_name:
Which will not check for equality but checks if check_con appears within the dictionary keys. However if this is what you want you have to loop over your items and find the intended one. But also note that this may have multiple answers or in other words there may be multiple keys matching your criteria which contradicts with the whole purpose of using a dictionary.
Demo:
In [11]: get_nested_item(tree, "George")
Out[11]: dict_values([{'Mickey'}])
In [12]: get_nested_item(tree, "Luke")
Out[12]: dict_values([{'The Chancellor'}])
In [13]: get_nested_item(tree, "Sarah")
No result
this is a variant where i use next(iter(...)) in order to get the 'first' element of your dict and set (note that you innermost curly brackets in your tree are sets and not dicts):
def get(tree, name):
def first(seq):
return next(iter(seq))
if name in tree:
return first(first(tree[name].values()))
else:
return None
print(get(tree=tree, name='Juan')) # 'Thompson'
print(get(tree=tree, name='Jan')) # None
as both sets and dict_values (which is the type dict(...).values() returns) are not indexable (have no __getitem__ method) i turn them into an iterator using iter and get the first element using next.
python 3.5.1
hi i have following json and python code and i want to print json data but it has an error that says:
Key Error : 'A'
python
data = json.load(...)
for item in data['x']
print (item['A'])
json
{"x":[
{"A":"B"},
{"C":"D"}
]}
whats my problem?
To print the values in each dictionary (with unmatching keys), use the values method of the dictionary:
data = json.load(...)
for item in data['x']:
print(item.values())
The problem is that your code assumes that every item in data['x'] will have a key 'A', but as soon as you iterate to a dict that does not have such a key you will get a KeyError.
Try using item.get('A') which will return None (or a default you provide) if there is no key 'A' in your dictionary. It seems like you want to do something like this:
data = json.load(...)
for item in data['x']:
value = item.get('A')
if value:
print(value)
else:
continue
This will print the value associated with the key 'A' if it exists, otherwise it will move on to the next dictionary in the list.
As #elethan pointed the second item would not have key 'A'
You can do the following
data = json.load(...)
for item in data['x']:
print(item.get('A'))
That would not get any error for your specific json input, and print None if it won't find 'A' key in element.
You can also supply default value to .get(), e.g. item.get('A', 'default').
Thanks #elethan
Is there a simple way to create a dictionary from a list of formatted tuples. e.g. if I do something like:
d={"responseStatus":"SUCCESS","sessionId":"01234","userId":2000004904}
This creates a dictionary called d. However, if I want to create a dictionary from a string which contains the same string, I can't do that
res=<some command that returns {"responseStatus":"SUCCESS","sessionId":"01234","userId":2000004904}>
print res
# returns {"responseStatus":"SUCCESS","sessionId":"01234","userId":2000004904}
d=dict(res)
This throws an error that says:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
I strongly strongly suspect that you have json on your hands.
import json
d = json.loads('{"responseStatus":"SUCCESS","sessionId":"01234","userId":2000004904}')
would give you what you want.
Use dict(zip(tuples))
>>> u = ("foo", "bar")
>>> v = ("blah", "zoop")
>>> d = dict(zip(u, v))
>>> d
{'foo': 'blah', 'bar': 'zoop'}
Note, if you have an odd number of tuples this will not work.
Based on what you gave is, res is
# returns {"responseStatus":"SUCCESS","sessionId":"01234","userId":2000004904}
So the plan is to grab the string starting at the curly brace to the end and use json to decode it:
import json
# Discard the text before the curly brace
res = res[res.index('{'):]
# Turn that text into a dictionary
d = json.loads(res)
All you need to do in your particular case is
d = eval(res)
And please keep security in mind when using eval, especially if you're mixing it with ajax/json.
UPDATE
Since others pointed out you might be getting this data over the web and it isn't just a "how to make this work" question, use this:
import json
json.loads(res)
I am very new to Python and parsing data.
I can pull an external JSON feed into a Python dictionary and iterate over the dictionary.
for r in results:
print r['key_name']
As I walk through the results returned, I am getting an error when a key does not have a value (a value may not always exist for a record). If I print the results, it shows as
'key_name': None, 'next_key':.................
My code breaks on the error. How can I control for a key not having a value?
Any help will be greatly appreciated!
Brock
The preferred way, when applicable:
for r in results:
print r.get('key_name')
this will simply print None if key_name is not a key in the dictionary. You can also have a different default value, just pass it as the second argument:
for r in results:
print r.get('key_name', 'Missing: key_name')
If you want to do something different than using a default value (say, skip the printing completely when the key is absent), then you need a bit more structure, i.e., either:
for r in results:
if 'key_name' in r:
print r['key_name']
or
for r in results:
try: print r['key_name']
except KeyError: pass
the second one can be faster (if it's reasonably rare than a key is missing), but the first one appears to be more natural for many people.
There are two straightforward ways of reading from Python dict if key might not be present. for example:
dicty = {'A': 'hello', 'B': 'world'}
The pythonic way to access a key-value pair is:
value = dicty.get('C', 'default value')
The non-pythonic way:
value = dicty['C'] if dicty['C'] else 'default value'
even worse:
try:
value = dicty['C']
except KeyError as ke:
value = 'default value'
If possible, use the simplejson library for managing JSON data.
the initial question in this thread is why I wrote the Dictor library, it handles JSON fallback and None values gracefully without needing try/except or If blocks.
Also gives you additional options like ignore upper/lower case,
see,
https://github.com/perfecto25/dictor
use has_key() , and that will return true or false
[Updated to remove careless mistake]
You could also do something like this:
for r in (row for row in results if 'a' in row):
print r['a']
This uses a generator expression to pick "rows" out of "results" where "row" includes the key "a".
Here's a little test script:
results = [{'a':True}, {'b':True}, {'a':True}]
for r in (row for row in results if 'a' in row): print r['a']
You can use the built in function hasattr
key='key_name'
# or loop your keys
if hasattr(e, key):
print(e[key])
else:
print('No key for %s' % key)
Taken from https://stackoverflow.com/a/14923509/1265070
id = getattr(myobject, 'id', None)