Skip to content

Instantly share code, notes, and snippets.

@sbliven
Created April 30, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbliven/c2777d07944be6040724bdfc39e72723 to your computer and use it in GitHub Desktop.
Save sbliven/c2777d07944be6040724bdfc39e72723 to your computer and use it in GitHub Desktop.
import shapefile
def replace_escape(s):
return s.replace(b'\xc3\x82\xc2',b'\xc2').replace(b'\xc3\x83\xc2',b'\xc3')
sf = shapefile.Reader("swisscantonsmod/ch-cantons.dbf")
print(f"records: {len(sf.records())}")
print(f"shapes: {len(sf.shapes())}")
out = shapefile.Writer("swisscantonsmod/ch-cantons_fixed.dbf")
for f in sf.fields:
out.field(*f)
def clean_rec(rec):
return tuple(replace_escape(r.encode()).decode() if isinstance(r, str) else r for r in rec)
for rec in sf.records():
out.record(*clean_rec(rec))
for shape in sf.shapes():
out.shape(shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment