如何跳过Excel工作表的前几行? [英] How can I skip first several lines of the Excel sheet?

查看:757
本文介绍了如何跳过Excel工作表的前几行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用openpyxl我试图从第五行读取某些文件.文件的前四行是标题.然后,主要内容的格式与标题不同.我尝试了该方法:

Using openpyxl I tried to read from the fifth line for some files. The files' first four lines are the header. Then the main content has a different format from the header. And I tried the method:

import openpyxl
file_name="xxx.xlsx"
wb = openpyxl.load_workbook(filename=file_name, use_iterators = True)
first_sheet = workbook.get_sheet_names()[0]
ws = workbook.get_sheet_by_name(first_sheet)

for index, row in enumerate(ws.iter_rows()):
    if start < index < stop:
        for c in row:
           print c.value

它将始终出现错误:

IndexError:列表索引超出范围

IndexError: list index out of range

如果删除前四行,则可以轻松地将数据读入Python.但是我有数百个这样的文件,每个文件都有一个四行的标题.从文件中删除所有标头将花费很多时间.

If I delete the first four lines, the data can be read into Python easily. But I have hundreds of such files, each file has a header for four lines. It will take way much time to delete all the headers from the files.

正确使用openpyxl进行阅读时,如何跳过前几行?

How to skip first several lines when reading using openpyxl correctly?

推荐答案

您可以将范围传递给ws.iter_rows('A4:Z256'),但最好使用ws.get_squared_range(1, 5,)

You can pass a range into ws.iter_rows('A4:Z256') but you're probably better off using ws.get_squared_range(1, 5,)

这篇关于如何跳过Excel工作表的前几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆