English 中文(简体)
I m getting getting
原标题:I m getting Key error in python

在我的假日方案中,我正在发现这一错误:

KeyError:  variablename 

根据该法典:

path = meta_entry[ path ].strip( / ),

谁能解释为什么发生这种情况?

最佳回答

A KeyError 通常是指存在关键吨数。 因此,您是否相信<代码>path 关键?

摘自官方书目:

http://www.ohchr.org。

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_enter/code>的内容,并检查是否存在<代码>path<>/code>。

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

或者,你可以做到:

>>>  a  in mydict
True
>>>  c  in mydict
False
问题回答

字典,公正使用

在关键清单中不使用搜索

<代码>关键词()

后者将耗费更多的时间。

是的,这很可能是由非重要因素造成的。

In my program, I used setdefault to mute this error, for efficiency concern. depending on how efficient is this line

>>> a  in mydict.keys()  

I am new to Python too. In fact I have just learned it today. So forgive me on the ignorance of efficiency.

在Adhur3,你也可以使用这一功能。

get(key[, default]) [function doc][1]

据称,这永远不会造成重大错误。

如果你重新使用3枚空洞,那就让我们简单化。

mydict = { a : apple , b : boy , c : cat }
check =  c  in mydict
if check:
    print( c key is present )

如果需要,

mydict = { a : apple , b : boy , c : cat }
if  c  in mydict:
    print( key present )
else:
    print( key not found )

为了具有活力的关键价值,你也可以通过审判例外处理。

mydict = { a : apple , b : boy , c : cat }
try:
    print(mydict[ c ])
except KeyError:
    print( key value not found )
    mydict = { a : apple , b : boy , c : cat }

我收到这一错误,当时我对<条码>的编码<>/条码>附后<条码>。

cats = { Tom : { color :  white ,  weight : 8},  Klakier : { color :  black ,  weight : 10}}
cat_attr = {}
for cat in cats:
    for attr in cat:
        print(cats[cat][attr])

追查:

Traceback (most recent call last):
      File "<input>", line 3, in <module>
    KeyError:  K 

由于第二版应为<条码>目录[目录],而仅限<条码>。 (只有钥匙)

因此:

cats = { Tom : { color :  white ,  weight : 8},  Klakier : { color :  black ,  weight : 10}}
cat_attr = {}
for cat in cats:
    for attr in cats[cat]:
        print(cats[cat][attr])

Gives

black
10
white
8

这意味着你的独裁者没有你所期待的关键。 我处理这一职能时,如果存在这种价值,或者收回一个违约价值,就能够收回价值。

def keyCheck(key, arr, default):
    if key in arr.keys():
        return arr[key]
    else:
        return default


myarray = { key1 :1,  key2 :2}

print keyCheck( key1 , myarray,  #default )
print keyCheck( key2 , myarray,  #default )
print keyCheck( key3 , myarray,  #default )

产出:

1
2
#default

例如,如果是:

ouloulou={
    1:US,
    2:BR,
    3:FR
    }
ouloulou[1]()

它的工作是完美的,but 如果你使用:

ouloulou[input("select 1 2 or 3"]()

由于你的投入回馈表明1点,因此它不工作。 因此,你需要使用<>int()。

ouloulou[int(input("select 1 2 or 3"))]()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签