gpt4 book ai didi

python - 需要处理keyerror异常python

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:24 24 4
gpt4 key购买 nike

当我在此处输入不在记录列表中的播放器名称时,出现键盘错误异常。我可以搜索它并取回任何有效名称,但如果我输入任何其他内容,我会收到键盘错误。我不太确定如何处理这个问题,因为它已经在处理通过解析我的文件创建的 3 组数据时有点令人困惑。

我知道这段代码很糟糕,我是 python 的新手,所以请原谅我的困惑——另请注意,这是一种让此功能正常工作的测试文件,然后我将把它写入我真正的主文件中的函数。有点像这里的试验台,如果这有意义的话。

这是我的数据文件 stats4.txt 中的内容:

[00000] Cho'Gath - 12/16/3 - Loss - 2012-11-22
[00001] Fizz - 12/5/16 - Win - 2012-11-22
[00002] Caitlyn - 13/4/6 - Win - 2012-11-22
[00003] Sona - 4/5/9 - Loss - 2012-11-23
[00004] Sona - 2/1/20 - Win - 2012-11-23
[00005] Sona - 6/3/17 - Loss - 2012-11-23
[00006] Caitlyn - 14/2/16 - Win - 2012-11-24
[00007] Lux - 10/2/14 - Win - 2012-11-24
[00008] Sona - 8/1/22 - Win - 2012-11-27

这是我的代码:

import re

info = {}
records = []
search = []
with open('stats4.txt') as data:
for line in data:
gameid = [item.strip('[') for item in line.split(']')]
del gameid[-1]
gameidstr = ''.join(gameid)
gameid = gameidstr
line = line[7:]
player, stats, outcome, date = [item.strip() for item in line.split('-', 3)]
stats = dict(zip(('kills', 'deaths', 'assists'), map(int, stats.split('/'))))
date = tuple(map(int, date.split('-')))
info[player] = dict(zip(('gameid', 'player', 'stats', 'outcome', 'date'), (gameid, player, stats, outcome, date)))
records.append(tuple((gameid, info[player])))

print "\n\n", info, "\n\n" #print the info dictionary just to see
champ = raw_input() #get champion name
#print info[champ].get('stats').get('kills'), "\n\n"
#print "[%s] %s - %s/%s/%s - %s-%s-%s" % (info[champ].get('gameid'), champ, info[champ].get('stats').get('kills'), info[champ].get('stats').get('deaths'), info[champ].get('stats').get('assists'), info[champ].get('date')[0], info[champ].get('date')[1], info[champ].get('date')[2])
#print "\n\n"
#print info[champ].values()

i = 0
for item in records: #this prints out all records
print "\n", "[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (records[i][0], records[i][1]['player'], records[i][1]['stats']['kills'], records[i][1]['stats']['deaths'], records[i][1]['stats']['assists'], records[i][1]['outcome'], records[i][1]['date'][0], records[i][1]['date'][1], records[i][1]['date'][2])
i = i + 1

print "\n" + "*" * 50
i = 0
for item in records:
if champ in records[i][1]['player']:
search.append(records[i][1])
else:
pass
i = i + 1

s = 0

if not search:
print "no availble records" #how can I get this to print even if nothing is inputted in raw_input above for champ?


print "****"
for item in search:
print "\n[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (search[s]['gameid'], search[s]['player'], search[s]['stats']['kills'], search[s]['stats']['deaths'], search[s]['stats']['assists'], search[s]['outcome'], search[s]['date'][0], search[s]['date'][1], search[s]['date'][2])
s = s + 1

我尝试设置一个 Try;除了某种东西,但输入无效的玩家名称时我无法得到任何不同的结果。我想我可能可以用一个函数来设置一些东西,并在名称存在与否的情况下返回不同的东西,但我想我只是让自己有点困惑。另请注意,对于 8 条不匹配的记录,确实不会打印任何匹配项,尽管这并不是我希望它工作的方式。基本上我需要为任何无效的输入名称获取类似的东西,而不仅仅是恰好不在循环记录中的有效输入。

此数据的有效输入名称是:Cho'Gath、Fizz、Caitlyn、Sona 或 Lux - 其他任何东西都会出现键盘错误,这就是我需要处理的,因此它不会引发错误,而只是打印类似“没有该冠军可用的记录”之类的内容(并打印只有一次,而不是 8 次)

感谢您的帮助!

[edit] 我终于能够在帖子中更新此代码(感谢 martineau 将其添加进来,由于某些原因反引号无法阻止代码并且它显示为粗体普通文本当我粘贴时。无论如何,看看 if not search,即使根本没有输入任何内容,我怎样才能打印它?只需在 raw_input 上按 return,目前它会打印 * 之后的所有记录*** 即使我没有给它任何搜索冠军

最佳答案

您的确切错误发生在哪里?

我只是假设它是 champ = raw_input() #get champion name

然后是 info[cham​​p]

您可以先检查 key 是否存在

if champ not in info:
print 'no records avaialble'

或使用get

if info.get(champ)

或者您可以尝试访问 key

try:
info[champ]
# do stuff
except KeyError:
print 'no records available'

你的问题越具体越好,尽管你解释了你的问题,但你确实没有包含任何细节请始终包含回溯(如果可用),并将相关代码发布在你的帖子中而不是链接上。

关于python - 需要处理keyerror异常python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13606426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com