3

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_SHP\TracksLines.shp")

Create a new shapefile

w = shapefile.Writer(r"C:...\Edit_SHP\TracksLines_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

Noura
  • 3,429
  • 3
  • 20
  • 41
user179413
  • 43
  • 2

0 Answers0