I am trying to find a fast way to merge two shapefiles and i ran over pyshp which apparently could do the job. However looks like this is not working for me because i receive the following error
file "c:\net\shapefile.py" ine 443 in __dbfHeader
fieldDesc = list(unpack("<11sc4xBB14x", dbf.read(32))) error: unpack requires a string argument of length 32.
I have no idea what could be wrong and i've been trying to find a solution but no luck
My code looks like this:
import glob
import shapefile
import sys
sys.path.append('C:\NET')
files = glob.glob("C:\TestSHAPE\merge\*.shp")
w = shapefile.Writer()
for f in files:
r = shapefile.Reader(f)
w._shapes.extend(r.shapes())
w.records.extend(r.records())
w.fields = list(r.fields)
w.save("merged")
Best Answer
The error message seems to suggest that there could be a fault in the attributes file of one of your shapefiles or with the pyshp module itself (perhaps corrupted during download). However, there are also some issues with your code that is muddying the waters.
files = glob.glob(r"C:\TestSHAPE\merge\*.shp")
If your process works it will only contain the last shapefile as you need to move two of your lines to within the loop like this:
You have not specified a path for the output. This means it will most likely be saved to the Python root folder (e.g.
C\Python32\merged.shp
). This is probably not what you want.You need to check your shapefiles to be sure that they are not the source of the error. Try opening them in QGIS or something. Next correct your code. Your coding errors by themselves do not account for the error message which is why I am concentrating on either a bad copy of pyshp or a bad shapefile. However, they don't help and if you do track down the problem, your code in the current state will not work as intended.
Finally check out this post which is similar: Pyshp 1.2 example from documentation fails