【发布时间】:2015-11-19 16:53:08
【问题描述】:
我正在尝试使用 Python 将从大型 Excel 工作簿中检索到的输出写入另一个电子表格。但是,我做不到,它给了我诸如 raise ValueError("column index (%r) not an int in range(256)" % arg)ValueError: column index (256) not an int in range(256) 之类的错误),异常:意外的数据类型。 我可以在一定程度上理解这些错误,但无法纠正我的代码。我在这里写了一个小脚本。如果有人能告诉我并纠正我哪里出错了,那就太好了。
import xlrd
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "path/lookup_V1.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
print sheet1.name
sheet2 = workbook.sheet_by_index(1)
print sheet2.name
print workbook.nsheets
st1c=sheet1.ncols
st2r=sheet2.nrows
st1=st1c+1
st2=st2r+1
print("fill..")
for i in xrange(0,st1c):
s1=sheet1.col_values(i)
i+1
s1.sort()
print s1
for col in xrange(st1c):
for row in xrange(st2r):
print("filling sheet...")
col=col+1
row=row+1
ws.write(row,col)
print("here")
wb.save('testfile.xls')
【问题讨论】:
-
请包含整个回溯,以便我们查看它在哪一行。来回查看您的错误和您的代码并试图猜测计算机何时告诉您发生这种情况的确切位置并且您出于某种原因没有与我们分享,这并不有趣。
-
如果您的代码缩进正确也会有所帮助
-
C:\Python2lib\sitepackages\spyderlib\widgets\externalshell\sitecustomize.py",第 540 行,在运行文件 execfile(filename, namespace)"/Testing.py",第 40 行,在
ws.write(row,col)File "C:\Python27\lib\site-packages\xlwt\Worksheet.py",第 1030 行,写入 self.row(r).write(c, label, style)File "C:\Python27\lib\site-packages\xlwt\Row.py",第 235 行,写入 self.__adjust_bound_col_idx(col 文件 "C:\Python27\lib\site-packages\xlwt\Row.py",行78, in_adjust_bound_col_idxraiseValueError("列索引 (%r) 不是 int in range(256)" % arg)ValueError: column index (256) not an int in range(256) -
将内容从答案转移到评论:col 和 row 是索引号吗?
-
你不能做 st1=st1c+1 因为 st1c 还没有定义...