# Pyshp 1.2 example from documentation fails

I must be doing something really stupid because I am trying to run an example straight from the PyShp (shapefile.py) 1.2 documentation with the only change being the input shapefile and it is failing. Note that it is 32bit Python 2.6 on Server 2008 R2 64bit.

The code:

import shapefile
e = shapefile.Editor(shapefile="F:/data/IntermediateWorkspace/5day_pgn.shp")
e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]])
e.record("Appended","Polygon")
e.save('F:/data/IntermediateWorkspace/5day_pgn')


The error:

Traceback (most recent call last): File "F:\scripts\shapetest.py", line 2, in e = shapefile.Editor(shapefile="F:/data/IntermediateWorkspace/5day_pgn.shp") File "C:\Python26\ArcGIS10.0\lib\shapefile.py", line 1048, in init self.records = r.records() File "C:\Python26\ArcGIS10.0\lib\shapefile.py", line 520, in records self.__dbfHeader() File "C:\Python26\ArcGIS10.0\lib\shapefile.py", line 443, in __dbfHeader fieldDesc = list(unpack("<11sc4xBB14x", dbf.read(32))) error: unpack requires a string argument of length 32

Thanks for any help!!

• Based on the last line in the error, did you try to move the shapefile to a place where the path length would equal 32 characters or less, and try again? – Get Spatial Sep 22 '13 at 10:42
• Actually, scratch that first comment. If you open up the shapefile.py script, and look at line 443, which is referenced in the error, it is discussing the dbfheader. I think some portion of that, whether a field name, or some other is restricted to a certain length. I would try with any other shapefile that is simple, with one or two fields, and see if that works. – Get Spatial Sep 22 '13 at 11:02
• Also try just reading that shapefile with a reader object. The Editor class is just a convenience which reads in the shapefile and sets it up in a Writer class instance. The error is happening, as Get Spatial said, when the Reader object is trying to parse the dbf header. The dbf reader in pyshp is pretty strict. I'm working on making it more flexible as minor issues in dbf files seem to be the number 1 source of problems. If the Reader fails too then there's an issue with the dbf header. If not the Editor class is broken. – GeospatialPython.com Sep 22 '13 at 14:14
• Thanks for the recommendations. I had tried reducing the number of columns down to 2 and reducing the path length, but of course that did not help. As you guys noted in comments the problem is occurring while reading the dbf header. I will give the reader object a try. The code in shapefile.py where it is having trouble is 'numFields = (headerLength - 33) // 32 for field in range(numFields): fieldDesc = list(unpack("<11sc4xBB14x", dbf.read(32)))' – user2803687 Sep 24 '13 at 18:50
• Tried using reader and writer instead, and found that the fields get read in fine, but it fails when I try to write. Any idea why this fails: 'w = shapefile.Writer(shapefile.POLYGON) reader = shapefile.Reader(path) #Open input file for fld in reader.fields[1:]: print fld w.field(fld)' with this error: File "C:\Python26\ArcGIS10.0\lib\shapefile.py", line 730, in __dbfHeader if field[0].startswith("Deletion"): AttributeError: 'list' object has no attribute 'startswith' – user2803687 Sep 25 '13 at 18:26