Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不在for dict()中不起作用_Python_Python 3.x - Fatal编程技术网

Python 不在for dict()中不起作用

Python 不在for dict()中不起作用,python,python-3.x,Python,Python 3.x,这是我的密码。我只想在名为“country”的字典中记录键时的值,该值在字典中不存在。为此,我正在检查关键字notin(代码的最后一行)。但是,它不起作用。即使字典中存在键,它也会更新值。任何帮助都将不胜感激 df = pd.DataFrame() country = dict() for file in allFiles: df = pd.read_excel(file,'Internal', skiprows = 7) print ("file name is " + fil

这是我的密码。我只想在名为“country”的字典中记录键时的值,该值在字典中不存在。为此,我正在检查关键字notin(代码的最后一行)。但是,它不起作用。即使字典中存在键,它也会更新值。任何帮助都将不胜感激

df = pd.DataFrame()
country = dict()

for file in allFiles:

   df = pd.read_excel(file,'Internal', skiprows = 7)
   print ("file name is " + file)

   if(df.loc[1][0] ==  "Country:"):
      key = df.loc[1][1]
      value = df.loc[2][1]
   else:
      key = df.loc[2][1]
      value = df.loc[1][1]

   print ('key is ' , key)
   print ('value is ' , value)

   try:
       print (df.loc[47][2], df.loc[47][3])
       print (df.loc[49][2], df.loc[49][3])

   except Exception as inst:
       print (type(inst))
       print (inst.args)
       print (inst)


   if ((key not in country) or (value != 'nan')):
      country[key] = value

改变你的状况

# Even if 'key' was already in 'country', If (value != 'nan')
# evaluated to 'True', the update will happen
if ((key not in country) or (value != 'nan')):


由于
很可能不是
nan
,因此记录将始终更新。

# Even if 'key' was already in 'country', If (value != 'nan')
# evaluated to 'True', the update will happen
if ((key not in country) or (value != 'nan')):


由于
value
很可能不是
nan
,因此记录将始终更新。

请阅读if语句中的条件,并思考如果
value
不是nan会发生什么。请阅读if语句中的条件,并思考如果
value
不是nan会发生什么。