这是我的代码,我用它打开一个 excel 表,然后将每一行作为字符串列表返回(其中每个单元格都是一个字符串)。该类返回一个列表,该列表填充的列表与文件中的行数一样多。所以 50 行将返回 50 个列表。
from xlrd import open_workbook
class ExcelReadLines(object):
def __init__(self,path_to_file):
'''Accepts the Excel File'''
self.path_to_file = path_to_file
self.__work__()
def __work__(self):
self.full_file_as_read_lines = []
self.book = open_workbook(self.path_to_file)
self.sheet = self.book.sheet_by_index(0)
for row_index in range(self.sheet.nrows):
single_read_lines = []
for col_index in range(self.sheet.ncols):
cell_value_as_string = str(self.sheet.cell(row_index,col_index).value)
cell_value_stripped = cell_value_as_string.strip('u')
single_read_lines.append(cell_value_stripped)
self.full_file_as_read_lines.append(single_read_lines)
return self.full_file_as_read_lines
但是当我运行时:
for x in ExcelReader('excel_sheet'): print x
我收到错误消息:
class is not iterable