#!/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
3 个解决方案
#1
数组越界了。argv看有几个参数等
#2
列表越界了
检查下这段代码
比如这个list的大小只有5,而你使用了list[10],就会报集合索引越界。
检查下这段代码
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],就会报集合索引越界。
#3
数据越界,检查一下,索引是从0开始的。sys.argv[2]改成sys.argv[1],sys.argv[1]改成sys.argv[0]
#1
数组越界了。argv看有几个参数等
#2
列表越界了
检查下这段代码
比如这个list的大小只有5,而你使用了list[10],就会报集合索引越界。
检查下这段代码
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],就会报集合索引越界。
#3
数据越界,检查一下,索引是从0开始的。sys.argv[2]改成sys.argv[1],sys.argv[1]改成sys.argv[0]