python使用format出现tuple index out of range报错的可能原因及解决

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41672415/article/details/82889291

关于python 使用format格式化输出时出现IndexError: tuple index outof range报错的原因及解决方法

本人python小白,希望用博客记录下自己犯的错误供自己日后编程和大家编程中参考和防范。

上图是本人变成时出现报错的截图,该报错:tuple index out of range意为元组指数超过范围。在这里format里面的参数和前面的槽是一一对应的,因此当槽的个数大于参数时就会出现报错。解决方法如下图:

在槽内加入对应的编号即可

展开阅读全文

py转exe问题 出现tuple index out of range

06-07

我想把py转为exe文件。rnrn在网上找的方法,做setup.pyrn[code=python]rnfrom distutils.core import setuprnimport py2exernsetup(console=["killexe.py"])rn[/code]rnrn另外我的killexe.pyrn[code=python]import osrnos.system("taskkill /IM SogouCloud.exe /F")rnprint("complete")rn[/code]rnrn然后再win 控制台上执行python setup.py py2exern报下面的错误rnrn[code=python]rnE:\Python>python setup.py py2exernrunning py2exernTraceback (most recent call last):rn File "setup.py", line 3, in rn setup(console=["killexe.py"])rn File "C:\Program Files\lib\distutils\core.py", line 148, in setuprn dist.run_commands()rn File "C:\Program Files\lib\distutils\dist.py", line 966, in run_commandsrn self.run_command(cmd)rn File "C:\Program Files\lib\distutils\dist.py", line 985, in run_commandrn cmd_obj.run()rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\distutils_buildexe.py", line 188, in runrn self._run()rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\distutils_buildexe.py", line 267, in _runrn builder.analyze()rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\runtime.py", line 160, in analyzern self.mf.import_hook(modname)rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\mf3.py", line 120, in import_hookrn module = self._gcd_import(name)rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\mf3.py", line 274, in _gcd_importrn return self._find_and_load(name)rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\mf3.py", line 357, in _find_and_loadrn self._scan_code(module.__code__, module)rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\mf3.py", line 388, in _scan_codern for what, args in self._scan_opcodes(code):rn File "C:\Users\nasri.yang\AppData\Roaming\Python\Python37\site-packages\py2exe\mf3.py", line 417, in _scan_opcodesrn yield "store", (names[oparg],)rnIndexError: tuple index out of rangernrnE:\Python>[/code]rnrn这个应该代码本身没有问题,是不是 版本支持方面的问题?rn 论坛

String index out of range: -91 怎么解决

07-27

String files = "[2010-07-19 08:20:51] Uin=1072, UserName=林立, Addr=XXXX:1047, Action=login, Mode=local, Result=0(success)\n"rn+ "[2010-07-19 22:55:08] Uin=1017, UserName=伟文, Addr=XXXX:1070, Action=logout, Mode=local";rn rn String loginTime="";rn int uin=0;rn String userName="";rn String addr="";rn String action="";rn String mode="";rn String result="";rn String[] lines = files.split("\n");rn for(String line:lines)rn rn vo = new LogVO();rnrn int index1 = line.indexOf("[");rn int index2 = line.indexOf("]");rn loginTime = line.substring(index1+1, index2);rn rn int index3 = line.indexOf("Uin=");rn int index4 = line.indexOf(",", index3);rn uin = Integer.parseInt(line.substring(index3+4, index4));rnrn int index5 = line.indexOf("UserName=");rn int index6 = line.indexOf(",", index5);rn userName = line.substring(index5+9, index6);rnrn int index7 = line.indexOf("Addr=");rn int index8 = line.indexOf(",", index7);rn addr = line.substring(index7+5, index8);rnrn int index9 = line.indexOf("Action=");rn int index10 = line.indexOf(",", index9);rn action = line.substring(index9+7, index10);rnrn int index11 = line.indexOf("Mode=");rn int index12 = line.indexOf(",", index11);rn mode = line.substring(index11+5, index12);//到了这里就说下标越界了rn if(line.contains("Result="))rn rn int index13 = line.indexOf("Result=");rn int index14 = line.indexOf("\n", index13);rn result = line.substring(index13+7, 8);rn rn rn vo.setLoginTime(loginTime);rn vo.setUin(uin);rn vo.setUserName(userName);rn vo.setAddr(addr);rn vo.setAction(action);rn vo.setMode(mode);rn vo.setResult(result);rn list.add(vo);rn rnrn我现在是从日志里面解析出各个字段信息,但是到mode = line.substring(index11+5, index12);//这一行就报错:String index out of range: -91,下面一个result也是越界,这个要怎么改呢?rn 论坛

没有更多推荐了,返回首页