我不知道为什么。
我只想将 Excel 文件读入 Python 代码。这些代码有什么问题?我需要帮助。谢谢!
#test2.py
import xlrd
fname = "sample.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name("Sheet1")
except:
print "no sheet in %s named Sheet1" % fname
return None
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)
cell_value = sh.cell_value(1,1)
print cell_value
row_list = []
for i in range(1,nrows):
row_data = sh.row_values(i)
row_list.append(row_data)
错误是:
'return' outside function (D:/pdf-ex/test2.py,line 10)
最佳答案
您在第 10 行中使用了 return
语句。return
应该在 inside 函数中使用,您的代码不在函数内部.
添加
import sys
在程序的最开始和第 10 行,替换 return None
为
sys.exit(-1)
有关 return 的更多信息和 sys.exit()
关于python - 错误代码 : 'return' outside function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10747635/