file_name = files.blobstore.create(mime_type='application/ms-excel', _blobinfo_uploaded_filename='sample.xls')
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Sheet 1')
stringa = str(newfile.text)
s3 = stringa.split('\n')
i=1
for riga in s3:
s2=riga.split()
try:
x = float(s2[0])
y = float(s2[1])
sheet.write(i, 1, '%g' %x)
sheet.write(i, 2, '%14.3e' %y)
except:
sheet.write(i, 1, '%s' %s2[0])
sheet.write(i, 2, '%s' %s2[1])
i=i+1
文件“/base/data/home/apps/s~marco-busso/1.357756583016056739/helloworld.py”,第140行,邮寄 sheet.write(i,1,'%s'%s2 [0]) IndexError:列表索引超出范围
为什么?
答案 0 :(得分:1)
显然s2
为空,这意味着riga
是一个空字符串。 stringa
连续有多个'\n'
吗?检查newfile.text
,也许那里是空行。
为了防止错误,您可以将循环体包装在if
语句中,例如:
for riga in s3:
if riga:
s2=riga.split()
try:
x = float(s2[0])
y = float(s2[1])
sheet.write(i, 1, '%g' %x)
sheet.write(i, 2, '%14.3e' %y)
except:
sheet.write(i, 1, '%s' %s2[0])
sheet.write(i, 2, '%s' %s2[1])
i=i+1