Unicode and dictionaries

gizli mehmetf at gmail.com
Sat Jan 16 18:35:05 EST 2010


Hi all,

I am using Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41). I
ran into this issue yesterday and wanted  to check to see if this is a
python bug. It seems that there is an inconsistency between lists and
dictionaries in the way that unicode objects are handled. Take a look
at the following example:

>>> test_dict = {u'öğe':1}
>>> u'öğe' in test_dict.keys()
True
>>> 'öğe' in test_dict.keys()
True
>>> test_dict[u'öğe']
1
>>> test_dict['öğe']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '\xc3\xb6\xc4\x9fe'
>>>

Is this a bug? has_key functionality of the dictionary works as
expected:

>>> test_dict.has_key(u'öğe')
True
>>> test_dict.has_key('öğe')
False



More information about the Python-list mailing list