我正在使用xlrd来嗅探目录结构并拔出电子表格,将第二行(第1行)读到“做东西”。问题是我不知道如何在第一个空行停止读/打印。我知道行不是“空”对象,但我会感谢一些帮助,显示如何检查所有单元格是否为空。这是我正在使用的代码:
import xlrd
import os
def excel_file_filter(filename, extensions=['.xls', '.xlsx']):
return any(filename.endswith(e) for e in extensions)
def get_filenames(root):
filename_list = []
for path, subdirs, files in os.walk(root):
for filename in filter(excel_file_filter, files):
filename_list.append(os.path.join(path, filename))
return filename_list
spreadsheets = get_filenames('C:\\Temp')
for s in spreadsheets:
with xlrd.open_workbook(s) as wb:
cs = wb.sheet_by_index(0)
num_cols = cs.ncols
for row_index in range(1, cs.nrows):
print('Row: {}'.format(row_index))
for col_index in range(0, num_cols):
cell_object = cs.cell(row_index, col_index)
if cell_obj is not xlrd.empty_cell:
print('Col #: {} | Value: {}'.format(col_index, cell_obj))
最终发生的事情是它一直打印了近1000行,而只有第一个说,25行有内容。电子表格之间的内容量各不相同,因此可以理解一个通用解决方案(不依赖于其他可选库)来帮助我了解如何检测空行然后中断。
答案 0 :(得分:7)
首先:要获取单元格值,然后检查它是否为空,请使用问题How to detect if a cell is empty when reading Excel files using the xlrd library?的答案中解释的方法之一
cell_val= cs.cell(row_index, col_index).value
获取值时:
if cell_vel == ''
cell_object = cs.cell(row_index, col_index)
获取值时:
cell_type = cs.cell_type(row_index, col_index)
if cell_type == xlrd.XL_CELL_EMPTY
第二:要检查整行是否为空,您可以执行以下操作:
守则:
# define empty_cell boolean
empty_cell= False
with xlrd.open_workbook(s) as wb:
cs= wb.sheet_by_index(0)
num_cols= cs.ncols
num_rows= cs.nrows
for row_index in range(1, num_rows):
# set count empty cells
count_empty = 0
print('Row: {}'.format(row_index))
for col_index in range(0,num_cols):
# get cell value
cell_val= cs.cell(row_index, col_index).value
# check if cell is empty
if cell_val== '':
# set empty cell is True
empty_cell = True
# increment counter
count_empty+= 1
else:
# set empty cell is false
empty_cell= False
# check if cell is not empty
if not empty_cell:
# print value of cell
print('Col #: {} | Value: {}'.format(col_index, cell_val))
# check the counter if is = num_cols means the whole row is empty
if count_empty == num_cols:
print ('Row is empty')
# stop looping to next rows
break
注意:我使用第一种方法cell_val= cs.cell(row_index, col_index).value
来获取单元格的值,我觉得它更简单。
如果要使用其他方法,请更改以下内容:
cell_val= cs.cell(row_index, col_index) # remove .value
cell_type= cs.cell_type(row_index, col_index) # add this line
# check if cell is empty
if cell_type == xlrd.XL_CELL_EMPTY: # change if cell_val== '':
帮助我了解如何检查单元格是否为空的其他链接:
xlrd.XL_CELL_EMPTY和Validating a cell value using XLRD
答案 1 :(得分:0)
要检查单个单元格是否为空,请检查其ctype
属性。要检查整行,请在all
上使用list comprehension函数:
workbook = xlrd.open_workbook(filepath)
sheet = workbook.sheets()[0]
rows = sheet.get_rows()
next(rows) # skip first row
for row in rows:
if all([cell.ctype in (xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK)
for cell in row]):
break
# process this non-empty row here...