user5454232
user5454232

Reputation:

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')

Upvotes: 3

Views: 871

Answers (1)

Rockybilly
Rockybilly

Reputation: 4510

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.

Upvotes: 1

Related Questions