0

I'm iterating through a dataset with .iter_rows() :

for row in dataset.iter_rows():
    print row
    print

For a reason that isn't relevant to the question, I'd like to skip the first 20 rows, for instance. Is this possible in a cleaner way than this trick ?

skip = 20
i = 0
for row in dataset.iter_rows():
    i += 1
    if i <= skip:
        continue
    print row
    print
François M.
  • 4,027
  • 11
  • 30
  • 81
  • If this is an openpyxl `iter_rows()` method, then the `row_offset` would definitely be the best option. If not, then `islice()`. And let us know if it was mistakenly closed. Thanks. – alecxe Jan 11 '17 at 17:16

1 Answers1

2

You can use itertools.islice():

Make an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached.

from itertools import islice

for row in islice(dataset.iter_rows(), 20):
    # ...
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195