UTF-8字符串作为字典中的关键字引起KeyError [英] UTF-8 string as key in dictionary causes KeyError

查看:303
本文介绍了UTF-8字符串作为字典中的关键字引起KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典与unicode字符串作为键。当我尝试访问该值时,我会得到关键错误,即使字典和我的密钥中的键的打印输出相同:

 >>> test =Byggår
>>> key = raw_dict.keys()[7]
>>> print(test)
Byggår
>>>> print(key)
Byggår
>>> test
'Bygg\xc3\xa5r'
>>>键
u'Bygg\xe5r'
>>> raw_dict [test]
追溯(最近的最后一次调用):
文件/Applications/PyCharm.app/Contents/helpers/pydev/pydevd_exec.py,第3行,在Exec
文件< input>,第1行,< module>
KeyError:'Bygg\xc3\xa5r'

似乎他们被编码不同,不知何故。从实验看,似乎字典中的键被编码为八进制字节(?) http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=xc3+xa5&mode=obytes ,而我尝试访问该值的键被编码为十六进制(?) http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=xe5&mode=hex



字典中的键从Web来源获取,所以我想有一些东西在路上被弄乱了。

解决方案

您的测试是一个字符串,而是一个 unicode字符串。请参阅前面的 u



您应该使用Python 3,其中所有字符串都是Unicode字符串,或者在字典中查找之前确保将测试转换为unicode。


I have a dictionary with unicode strings as keys. When I try to access the value I get key error, even though the printout of the key in dictionary and my key are equal:

>>> test = "Byggår"
>>> key = raw_dict.keys()[7]
>>> print(test)
Byggår
>>> print(key)
Byggår
>>> test
'Bygg\xc3\xa5r'
>>> key
u'Bygg\xe5r'
>>> raw_dict[test]
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd_exec.py", line 3, in Exec
  File "<input>", line 1, in <module>
KeyError: 'Bygg\xc3\xa5r'

It seems as they are encoded differently, somehow. From experimenting it seems as the key in the dictionary is encoded as octal bytes (?) http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=xc3+xa5&mode=obytes, whilst the key I try to access the value with is encoded as hex(?) http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=xe5&mode=hex .

The keys in dictionary are fetched from a web source, so I guess something gets messed up on the way.

解决方案

Your test is a string while key is a unicode string. See the u in-front of it?

You should either use Python 3, where all strings are unicode strings, or make sure to convert test to unicode before looking for it in the dictionary.

这篇关于UTF-8字符串作为字典中的关键字引起KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆