KeyError虽然肯定在字典

时间:2016-11-30 03:59:07

标签: python jupyter-notebook keyerror

我有一个包含此内容的索引:

IRD\0.jpg [  4.64939594e-01   6.48846030e-02   2.00261129e-04       0.00000000e+00 0.00000000e+00  ...    7.34290807e-04   6.90233335e-02   2.02463999e-01]

但在这里打电话:

for (k, hist) in index.items():
    # compute the distance between the two histograms
    # using the method and update the results dictionary
    d = method(index['IRD\0.jpg'], hist)
    results[k] = d

它出现了这个错误:

KeyError Traceback (most recent call last)
<ipython-input-98-b7c782484164> in <module>()
# compute the distance between the two histograms
# using the method and update the results dictionary
d = method(index['IRD\0.jpg'], hist)
results[k] = d

我真的不知道为什么? 非常感谢帮助,如果我遗漏了一些明显的东西,我很抱歉,但我对此很新:)

1 个答案:

答案 0 :(得分:0)

这是因为\是字符串中的转义字符。如果您希望它被解释为字符串中的实际字符,那么您必须将其转义:

>>> print('x\0')
x
>>> print('x\\0')
x\0

所以将index['IRD\0.jpg']更改为index['IRD\\0.jpg']