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 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.