Created
April 30, 2021 20:01
-
-
Save sbliven/c2777d07944be6040724bdfc39e72723 to your computer and use it in GitHub Desktop.
Script to clean encoding for https://github.com/druedin/swisscantonsmod/issues/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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