且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我在python中遇到关键错误

更新时间:2023-02-19 17:47:30

A KeyError 通常意味着密钥不存在。所以,你确定路径密钥存在吗?

从官方的python文档:

From the official python docs:

异常KeyError


当映射(字典)键不是在
现有的密钥集中找到。

Raised when a mapping (dictionary) key is not found in the set of existing keys.

例如:

>>> mydict = {'a':'1','b':'2'}
>>> mydict['a']
'1'
>>> mydict['c']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'c'
>>>

所以,尝试打印 meta_entry的内容并检查路径是否存在。

So, try to print the content of meta_entry and check whether path exists or not.

>>> mydict = {'a':'1','b':'2'}
>>> print mydict
{'a': '1', 'b': '2'}

或者你可以做:

>>> 'a' in mydict
True
>>> 'c' in mydict
False