8

I would like to set up range of reading by shapefile.Reader. I couldn't find any suitable documentation. I would like to read rows 0-100. In Pandas or GeoPandas I use df.iloc[0:100], but what should I use here?

Code:

with shapefile.Reader(shapefile_path) as shp:
    total_rows = shp.numRecords
    for row_num, row in enumerate(shp.iterRecords()):
        print(row)

2 Answers 2

10

A generator is not subscriptable and iterRecords() returns a generator. Instead, use shapeRecords() (or records()). It gives you a list.

rows = shapefile.Reader(shapefile_path).shapeRecords()[0:100]

for row_num, row in enumerate(rows):
    print(row_num, row)
7

I have found the solution:

with shapefile.Reader(shapefile_path) as shp:
        total_rows = shp.numRecords
        for row_num, row in enumerate(shp.iterRecords()):
            if row_num < 0:
                pass
            elif row_num > 100:
                break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.