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,4373 gold badges21 silver badges41 bronze badges
asked Mar 24, 2021 at 8:58
3
  • Maybe this can help you: geospatialpython.com/2013/04/… Commented Mar 24, 2021 at 9:01
  • Just tried, but it doesn't work, or maybe I'm just doing something wrong. Commented Mar 24, 2021 at 10:30
  • look at the answers in Updating Attributes using Pyshp Commented Mar 24, 2021 at 11:23

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.