简体   繁体   English

KeyError虽然肯定在词典中

[英]KeyError though definitely in the Dictionary

I have an index that contains this: 我有一个包含这个的索引:

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]

but when called here: 但是在这里打电话:

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

it comes up with this error: 它出现了这个错误:

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

and I don't really get why? 我真的不明白为什么? Help would be highly appreciated and I'm sorry if I'm missing something obvious but I am fairly new to this :) 帮助将非常感激,如果我遗漏了一些明显的东西,我很抱歉,但我对这个很新:)

It's because \\ is the escape character in strings. 这是因为\\是字符串中的转义字符。 If you want it to be interpreted as an actual char in the string, then you have to escape it: 如果您希望将其解释为字符串中的实际char,则必须将其转义:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM