您好我有以下代码。当我运行getDetails函数时,尝试: 确实有效,但异常无法正常显示。
data_dict = {}
dict_list = []
def createNameDict(filename):
path = "C:\Users\user\Desktop"
basename = "ParkingData_Part2.txt"
filename = path + "//" + basename
file = open(filename)
contents = file.read()
print contents,"\n"
data_list = [lines.split(",") for lines in contents.split("\n")]
for line in data_list:
regNumber = line[0]
details = (line[1],line[2],line[3])
data_dict[regNumber] = details
print data_dict,"\n"
print data_dict.items(),"\n"
def getDetails(regNumber):
#if regNumber in data_dict:
try:
if regNumber in data_dict:
print data_dict[regNumber]
#print data_dict.get(regNumber)
except:
printNow(regNumber, "not in dictionary")
return
def addRegistration(regNumber, details):
try:
data_dict[regNumber] = details
except:
print regNumber, "Key Already exist"
#data_dict.update({regNumber:(details)})
return data_dict
我的输出是:
======= Loading Progam =======
>>> createNameDict("C:\Users\user\Desktop//ParkingData_Part2.txt")
EDF768, Bill Meyer, 2456, Vet_Parking
TY5678, Jane Miller, 8987, AgHort_Parking
GEF123, Jill Black, 3456, Creche_Parking
ABC234, Fred Greenside, 2345, AgHort_Parking
GH7682, Clara Hill, 7689, AgHort_Parking
JU9807, Jacky Blair, 7867, Vet_Parking
KLOI98, Martha Miller, 4563, Vet_Parking
ADF645, Cloe Freckle, 6789, Vet_Parking
DF7800, Jacko Frizzle, 4532, Creche_Parking
WER546, Olga Grey, 9898, Creche_Parking
HUY768, Wilbur Matty, 8912, Creche_Parking
{'HUY768': (' Wilbur Matty', ' 8912', ' Creche_Parking'), 'GH7682': (' Clara Hill', ' 7689', ' AgHort_Parking'), 'GEF123': (' Jill Black', ' 3456', ' Creche_Parking'), 'WER546': (' Olga Grey', ' 9898', ' Creche_Parking'), 'TY5678': (' Jane Miller', ' 8987', ' AgHort_Parking'), 'ABC234': (' Fred Greenside', ' 2345', ' AgHort_Parking'), 'KLOI98': (' Martha Miller', ' 4563', ' Vet_Parking'), 'EDF768': (' Bill Meyer', ' 2456', ' Vet_Parking'), 'JU9807': (' Jacky Blair', ' 7867', ' Vet_Parking'), 'DF7800': (' Jacko Frizzle', ' 4532', ' Creche_Parking'), 'ADF645': (' Cloe Freckle', ' 6789', ' Vet_Parking')}
[('HUY768', (' Wilbur Matty', ' 8912', ' Creche_Parking')), ('GH7682', (' Clara Hill', ' 7689', ' AgHort_Parking')), ('GEF123', (' Jill Black', ' 3456', ' Creche_Parking')), ('WER546', (' Olga Grey', ' 9898', ' Creche_Parking')), ('TY5678', (' Jane Miller', ' 8987', ' AgHort_Parking')), ('ABC234', (' Fred Greenside', ' 2345', ' AgHort_Parking')), ('KLOI98', (' Martha Miller', ' 4563', ' Vet_Parking')), ('EDF768', (' Bill Meyer', ' 2456', ' Vet_Parking')), ('JU9807', (' Jacky Blair', ' 7867', ' Vet_Parking')), ('DF7800', (' Jacko Frizzle', ' 4532', ' Creche_Parking')), ('ADF645', (' Cloe Freckle', ' 6789', ' Vet_Parking'))]
>>> getDetails("GFT245")
>>> getDetails("HUY768")
(' Wilbur Matty', ' 8912', ' Creche_Parking')
>>> getDetails("JU9807")
(' Jacky Blair', ' 7867', ' Vet_Parking')
>>> getDetails("GH7682")
(' Clara Hill', ' 7689', ' AgHort_Parking')
>>>
如果GFT245不在词典中,它应该显示 GFT245不在字典中 但是当我执行程序
时它不会那样答案 0 :(得分:2)
问题出在这里:
if regNumber in data_dict:
print data_dict[regNumber]
当您询问字典中是否存在regNumber
时,由于regNumber
不存在而您不会尝试对其进行索引,因此永远不会引发异常,因此不会引发KeyError
例外。
如果您想要引发异常,那么只需执行:
print data_dict[regNumber]
答案 1 :(得分:2)
在您的getDetails
函数中,except
部分永远不会触发,因为您在访问之前明确检查密钥是否在字典中,您可能希望将其重写为
if key in dict:
# The key exists and do something
else:
# The key doesn't exist, notify the user
或
try:
print dict[key]
except KeyError:
# The key doesn't exist, notify the user
我更喜欢第一种的样式,因为异常是昂贵的,当然如果你知道你只有少量的字典中不存在的键并且会导致异常,那么你可以优化第二种方式
应重新编写addRegistration
函数以测试密钥是否已存在并通知用户,否则添加数据,因为如果密钥已存在,它将不会发出警告或引发异常,只会更新/替换该密钥的数据。
答案 2 :(得分:1)
if regNumber in data_dict:
print data_dict[regNumber]
在访问data_dict
之前,您有条件。如果regNumber
不在dict
,那么它就不会尝试访问它。因此没有抛出异常。
答案 3 :(得分:0)
def getDetails(regNumber):
#if regNumber in data_dict:
try:
if regNumber in data_dict:
print data_dict[regNumber]
#print data_dict.get(regNumber)
except:
printNow(regNumber, "not in dictionary")
return
try
块中没有任何内容实际上失败。
为什么呢?由于if regNumber in data_dict
测试。如果代码不在字典中,它将永远不会尝试查找它。
也许你的意思是:
def getDetails(regNumber):
if regNumber in data_dict:
print data_dict[regNumber]
else:
printNow(regNumber, "not in dictionary")
答案 4 :(得分:0)
问题是
try:
if regNumber in data_dict:
print data_dict[regNumber]
#print data_dict.get(regNumber)
except:
printNow(regNumber, "not in dictionary")
如果regNumber
不在data_dict
,则不会发生异常。将其更改为:
try:
print data_dict[regNumber]
except:
printNow(regNumber, "not in dictionary")