“level = line[3].rstrip()”的列表索引超出范围错误。仔细检查了所有内容,我在显示之前打印了列表,并显示了超过 4 个值(我正在尝试获取其中的第四个)。下面的代码和回溯,谢谢!
代码:
async def lbcmd(ctx):
el = []
for file in os.scandir(lpath):
if file.path.endswith(".txt"):
with open(file, 'r') as f:
line = f.readlines()
level = line[3].rstrip()
xp = line[0].rstrip()
exp = {
"user": file.name.strip(".txt"),
"exp": xp,
"level": level
}
el.append(exp)
el.sort(reverse=True, key=operator.itemgetter('exp'))
desc = ("""
""")
for exp in el:
desc = desc + f"""Level {exp['level']} at {exp['exp']} exp - <@{exp['user']}>
"""
embed=discord.Embed(title=f"Level leaderboard", description=desc, color=0x2f3136)
await ctx.send(embed=embed)
追溯:
Traceback (most recent call last):
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\(A) Me\Coding\Python\jcjakec\Bots\conceptbot\conceptbot.py", line 144, in lbcmd
level = line[3].rstrip()
IndexError: list index out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\jakec\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range```