4

I use PyShp and shapefile.Reader to read shapefile files.

If I want to read a whole file I use:

with shapefile.Reader(file) as shp:
    for row_num, row in enumerate(shp.iterRecords()):
        print(row)

But I would like to read a single row from shapefile file with shapefile.Reader, for example row number 100. I read documentation and I didn't find any solution. How can I do that?

7

shapefile.Reader loads all data to the memory. After using shapefile.Reader("file_path") you can get a specific row by index using shapeRecords method which returns a list containing all rows, then, you can get the specific row.

```
import shapefile

with shapefile.Reader(file) as shp:
    row = shp.shapeRecords()[99] # row with index 99 (100th row)
```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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