Python 跳过Openpxyl中的第一行
嗨,我有以下代码Python 跳过Openpxyl中的第一行,python,openpyxl,Python,Openpyxl,嗨,我有以下代码 wb_obj = openpyxl.load_workbook(path, data_only=True) worksheet = wb_obj.get_sheet_by_name('Sheet1') for row_cells in worksheet.iter_rows(): for cell in row_cells: print('%s: cell.value=%s' % (cell, cell.value) ) 它工作得很好。但是,我不需要
wb_obj = openpyxl.load_workbook(path, data_only=True)
worksheet = wb_obj.get_sheet_by_name('Sheet1')
for row_cells in worksheet.iter_rows():
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value) )
它工作得很好。但是,我不需要它来显示来自第一行(即标题行)的数据。如何更改查询以排除第一行?只需使用函数
enumerate()
枚举行,然后获取两个不同变量中的索引和单元格值,如
for index, row_cells in enumerate(worksheet.iter_rows()):
if (index == 0):
continue
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value))
由于iter_行是一个生成器,不幸的是,我们无法切掉第一行[1],因此这是一种跳过第一行的简单但有效的方法
for i, row_cells in enumerate(worksheet.iter_rows()):
if i == 0:
continue
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value))
建议使用iter\u行
for row_cells in worksheet.iter_rows(min_row=2):
for cell in row_cells:
print('%s: cell.value=%s' % (cell, cell.value) )
您可以像这样对工作表进行切片
worksheet[2: worksheet.max_row]
您好,谢谢,但是
和
条件似乎导致语法无效@vinjay@BilalHabib我的坏习惯是用逗号代替逗号,这在文档中有所涉及。只需使用ws.iter\u行(min\u row=2)