Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

python - How can I skip first several lines of the Excel sheet?

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

It will always have the error:

IndexError: list index out of range

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...