Skip to content

Instantly share code, notes, and snippets.

@DenisCarriere
Last active October 16, 2015 02:55
Show Gist options
  • Save DenisCarriere/88beaf719eaf3a43bcb3 to your computer and use it in GitHub Desktop.
Save DenisCarriere/88beaf719eaf3a43bcb3 to your computer and use it in GitHub Desktop.
Read attributes as Dictionary
#!/usr/bin/python
# coding: utf8
import shapefile
# pip install pyshp
def process(infile):
shp = shapefile.Reader(infile)
# Get rid of extra garbage in field list
# Remove the deletion flag from field list
fields = list((i[0]) for i in shp.fields[1:])
# Loop inside each feature within the Shapefile
for feature in shp.iterShapeRecords():
json = dict((fields[k], v) for k, v in enumerate(feature.record))
print(json)
if __name__ == '__main__':
process('/home/ubuntu/data/Example.shp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment