python新手读写excel问题,感谢!
# -*-coding=utf-8 -*-
import xlrd
#data = xlrd.open_workbook('D:\\gaoht\\工作任务\\2017\\投诉退费数据\\6月退费单\\20170601.xlsx')
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception as e:
print(str(e))
#def excel_table_byindex(file= 'D:\\gaoht\\工作任务\2017\\投诉退费数据\\6月退费单\\a20170601.xlsx',colnameindex=0,by_index=0):
def excel_table_byindex(file= u'F:\\python\\src\\a201706.xlsx',colnameindex=0,by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows #行数
ncols = table.ncols #列数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
#def excel_table_byname(file= 'D:\\gaoht\\工作任务\2017\\投诉退费数据\\6月退费单\\a20170601.xlsx',colnameindex=0,by_name=u'Sheet1'):
def excel_table_byname(file= u'F:\\python\\src\\a201706.xlsx',colnameindex=0,by_name=u'Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nrows = table.nrows #行数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
def main():
tables = excel_table_byname()
for row in tables:
print(row)
if __name__=="__main__":
main()
源码如上,在dos下运行报错:
F:\python\src>python read_excel.py
[Errno 2] No such file or directory: 'F:\\python\\src\\a201706.xlsx'
Traceback (most recent call last):
File "read_excel.py", line 48, in <module>
main()
File "read_excel.py", line 43, in main
tables = excel_table_byname()
File "read_excel.py", line 30, in excel_table_byname
table = data.sheet_by_name(by_name)
AttributeError: 'NoneType' object has no attribute 'sheet_by_name'
请问要如何调整呢?