python:Unicode equal comparison未能将两个参数都转换为Unicode

2024-10-01 11:30:54 发布

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

我有一个dict,想在里面搜索。在

test = dict({u'K\xfcndigung' : 123})
tmpKey = str('K\xfcndigung')
print(test[tmpKey])

其结果是:

^{pr2}$

Tags: testdictprintstrpr2tmpkeyxfcndigung
1条回答
网友
1楼 · 发布于 2024-10-01 11:30:54

你好像在用Python2:

>>> test[tmpKey]

Warning (from warnings module):
  File "__main__", line 1
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    test[tmpKey]
KeyError: 'K\xfcndigung'

在Python2中,str不是unicode,与Python3相反,因此将tmpKey改为unicode对象:

^{pr2}$

相关问题 更多 >