TransWikia.com

Editing shapefile adding fields with values with pyshp

Geographic Information Systems Asked by user179413 on May 20, 2021

I’m trying to add some fields to a shapefile using pyshp. The code I have to far is:

import shapefile

# Read in our existing shapefile
r = shapefile.Reader(r"C:...Edit_SHPTracksLines.shp")

# Create a new shapefile
w = shapefile.Writer(r"C:...Edit_SHPTracksLines_edited.shp")

# Copy over the existing fields
w.fields = list(r.fields)

# Copy over the records
for shaperec in r.iterShapeRecords():
 w.record(*shaperec.record)
 w.shape(shaperec.shape)

w.field('test')

The issue is that when I try to add a new field all the records already present on the shape are messed up.

How can I keep the same numbers of records, when adding new fields?

Original attribute table

Edited shpfile

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP