Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with keys in unicode

Tags:

python

unicode

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'
like image 565
KoirN Avatar asked Jul 27 '12 19:07

KoirN


People also ask

How to convert Unicode to dict?

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("'", "\"") .

Can dictionary have string keys?

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!"}

How do I generate a list of values from a dictionary key?

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.

How do I find a dictionary key?

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().


2 Answers

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 printing your dictionary; you'll see the repr of each key which should show what the actual key looks like.

like image 116
Wooble Avatar answered Oct 27 '22 02:10

Wooble


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.

like image 33
Bennett Brown Avatar answered Oct 27 '22 02:10

Bennett Brown