求助:IndexError: list index out of range

hahaha170127 2018-08-21 12:46:02
使用Spyder(python 3.6)代码为:
#!/usr/bin/env python3
import sys

from xlrd import open_workbook
from xlwt import Workbook

input_file = sys.argv[1]
output_file = sys.argv[2]

output_workbook = Workbook()
output_worksheet = output_workbook.add_sheet('jan_2013_output')

with open_workbook(input_file) as workbook:
worksheet = workbook.sheet_by_name('january_2013')
for row_index in range(worksheet.nrows):
for column_index in range(worksheet.ncols):
output_worksheet.write(row_index, column_index, worksheet.cell_value(row_index, column_index))
output_workbook.save(output_file)

执行:python 2excel_parsing_and_write.py sale_2013 Loutput.xls

出现问题,输出;
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 6.1.0 -- An enhanced Interactive Python.



runfile('C:/LearnPython/excel/2excel_parsing_and_write.py', args='sales_2013.xlsx', wdir='C:/LearnPython/excel')
Traceback (most recent call last):

File "<ipython-input-1-fcfc762a49a2>", line 1, in <module>
runfile('C:/LearnPython/excel/2excel_parsing_and_write.py', args='sales_2013.xlsx', wdir='C:/LearnPython/excel')

File "H:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)

File "H:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/LearnPython/excel/2excel_parsing_and_write.py", line 8, in <module>
output_file = sys.argv[2]

IndexError: list index out of range
...全文
640 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38298356 2018-08-22
  • 打赏
  • 举报
回复
数据越界,检查一下,索引是从0开始的。sys.argv[2]改成sys.argv[1],sys.argv[1]改成sys.argv[0]
oyljerry 2018-08-21
  • 打赏
  • 举报
回复
数组越界了。argv看有几个参数等
尧十三decode 2018-08-21
  • 打赏
  • 举报
回复
列表越界了
检查下这段代码
for row_index in range(worksheet.nrows):
for column_index in range(worksheet.ncols):
output_worksheet.write(row_index, column_index, worksheet.cell_value(row_index, column_index))
output_workbook.save(output_file)

比如这个list的大小只有5,而你使用了list[10],就会报集合索引越界。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧