Is it possible in Python to use Unicode characters as keys for a dictionary? I have Cyrillic words in Unicode that I used as keys. When trying to get a value by a key, I get the following traceback:
Traceback (most recent call last):
File "baseCreator.py", line 66, in <module>
createStoresTable()
File "baseCreator.py", line 54, in createStoresTable
region_id = regions[region]
KeyError: u'\u041c\u0438\u043d\u0441\u043a/\u041c\u043e\u0441\u043a\u043e\u0432\u0441\u043a\u0438\u0439\xa0'
To convert a Unicode string representation of a dict to a dict, use the json. loads() method on the string. However, the JSON library requires you to first replace all single quote characters with escaped double-quote characters using the expression s. replace("'", "\"") .
The keys of a dictionary can be any kind of immutable type, which includes: strings, numbers, and tuples: mydict = {"hello": "world", 0: "a", 1: "b", "2": "not a number" (1, 2, 3): "a tuple!"}
To get the list of dictionary values from the list of keys, use the list comprehension statement [d[key] for key in keys] that iterates over each key in the list of keys and puts the associated value d[key] into the newly-created list.
You can check if a key exists or not in a dictionary using if-in statement/in operator, get(), keys(), handling 'KeyError' exception, and in versions older than Python 3, using has_key().
Yes, it's possible. The error you're getting means that the key you're using doesn't exist in your dictionary.
To debug, try print
ing your dictionary; you'll see the repr of each key which should show what the actual key looks like.
Python 2.x converts both keys to bytestrings when comparing two keys for the purposes of testing whether a key already exists, accessing a value, or overwriting a value. A key can be stored as Unicode, but two distinct Unicode strings cannot both be used as keys if they reduce to identical bytestrings.
In []: d = {'a': 1, u'a': 2}
In []: d
Out[]: {'a': 2}
You can use Unicode keys, in some sense.
Unicode keys are retained in Unicode:
In []: d2 = {u'a': 1}
In []: d2
Out[]: {u'a': 1}
You can access the value with any Unicode string or bytestring that "equals" the existing key:
In []: d2[u'a']
Out[]: 1
In []: d2['a']
Out[]: 1
Using the key or anything that "equals" the key to write a new value will succeed and retain the existing key:
In []: d2['a'] = 5
In []: d2
Out[]: {u'a': 5}
Because comparing 'a'
to an existing key was True
, the value corresponding to that existing Unicode key was replaced with 5
. In the initial example I give, the second key u'a'
provided in the literal for d
compares truthfully to the previously assigned key, so the bytestring 'a'
was retained as the key but the value was overwritten with the 2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With