2019年1月31日 22:24 by wst
python小技巧1, 首先自己写好一个模块如:read_file_class.py
路径为:/home/wst/mylib/python-lib,
内容如下:
#!/usr/bin/env python
import xlrd
#Function:Read records from xls file.
#Depending lib:xlrd
class ReadXlsFile(object):
def __init__(self,filename):
self.filename=filename
def fetch_records(self):
wb=xlrd.open_workbook(self.filename)
sheet=wb.sheets()[0]
records=[]
for i in range(1,sheet.nrows):
records.append(sheet.row_values(i))
return records
2, 当你想要引用这个模块里的类的时候,方式如下:
import sys
sys.path.append(r'/home/wst/mylib/python-lib')
from read_file_class import ReadXlsFile
obj=ReadXlsFile('somefile.xls')
records=obj.fetch_records()
参考:http://jingyan.baidu.com/article/e75057f2acd380ebc91a89f0.html