字典中的“TypeError:”unicode“对象不支持项分配”

2024-05-13 08:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图建立/更新字典。我的昵称是temp_dict中的键,正在寻找要添加的id。

我的代码摘录。我想这足以让你看到我的错误。

d1 = {u'status': u'ok', u'count': 1, u'data': [{u'nickname': u'45sss', u'account_id': 553472}]}


   temp_dict = {}
   for key, value in d1.iteritems():
        if "data" == key:
            for dic2 in value:
                  x = dic2['nickname']
                  y = dic2['account_id']
                  temp_dict[x] = y;

我的错误:

Traceback (most recent call last):
File "untitled.py", line 36, in <module>
get_PlayerIds_Names_WowpApi_TJ_() #Easy going. Some issues with case letters.
File "g:\Desktop\Programming\WOWP API\functions.py", line 44, in get_PlayerIds_Names_WowpApi_TJ_
check_missing_player_ids(basket)
File "g:\Desktop\Programming\WOWP API\functions.py", line 195, in check_missing_player_ids
temp_dict[x] = y;
TypeError: 'unicode' object does not support item assignment

同一错误有多个SO条目。但没有人与这种字典操作有关联。


Tags: inpyidfordata字典错误line
1条回答
网友
1楼 · 发布于 2024-05-13 08:29:57

很有可能你把unicode字符串放在temp_dict的某个地方:

>>> temp_dict = u''
>>> dic2 = {u'nickname': u'45sss', u'account_id': 553472}
>>> x = dic2['nickname']
>>> y = dic2['account_id']
>>> temp_dict[x] = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'unicode' object does not support item assignment

用空的dict初始化它,所有的都将工作:

>>> temp_dict = {}
>>> temp_dict[x] = y
>>> temp_dict
{u'45sss': 553472}

相关问题 更多 >