gpt4 book ai didi

python - 字典中的 "TypeError: ' unicode ' object does not support item assignment"

转载 作者:太空狗 更新时间:2023-10-29 18:26:36 34 4
gpt4 key购买 nike

我正在尝试构建/更新字典。我将昵称作为 temp_dict 中的键并寻找要添加的 ID。

摘 self 的代码。我认为你看到我的错误就足够了。

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 条目。但没有人与这种字典操作有关。

最佳答案

很可能你已经把 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

用空字典初始化它,一切都会工作:

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

关于python - 字典中的 "TypeError: ' unicode ' object does not support item assignment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416460/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com