Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Unicode Values in a Python Dictionary

I have a dictionary full of unicode keys/values due to importing JSON through json.loads().

dictionaryName = {u'keyName' : u'valueName'}

I'm trying to access values inside the dictionary as follows:

accessValueName = dictionaryName.get('keyName')

This returns None, assumedly because it is looking for the String 'keyName' and the list is full of unicode values. I tried sticking a 'u' in front of my keyName when making the call, but it still returns none.

accessValueName = dictionaryName.get(u'keyName')

I also found several seemingly outdated methods to convert the entire dictionary to string values instead of unicode, however, they did not work, and I am not sure that I need the entire thing converted.

How can I either convert the entire dictionary from Unicode to String or just access the values using the keyname?

EDIT:

I just realized that I was trying to access a value from a nested dictionary that I did not notice was nested.

The solution is indeed:

accessValueName = dictionaryName.get('keyName')

1 Answers

Dictionaries store values in a hash table using the hash values of the object.

print(hash(u"example"))
print(hash("example"))

Yields the same result. Therefore the same dictionary value should be accessible with both.

like image 78
Rockybilly Avatar answered Dec 03 '25 20:12

Rockybilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!