[Tutor] Issue with a shapefile (ArcGIS) library (pyshp) "unpack requires a string argument of length 8"

Dave Angel d at davea.name
Fri Feb 3 17:26:56 CET 2012


On 02/03/2012 10:46 AM, Simeon Tesfaye wrote:
> Hello everyone,
>
Two thoughts, but realize I don't know anything about pyshp.
> I am having a bit of trouble here with my code, which uses a shapefile library, named pyshp, to import, edit, and save GIS files within Python.
> So, I open up my shapefile (data is polylines, meaning, not points or polygons)
> "shapefile=shapefile.Reader("file.shp")
> shps=shapefile.shapes()
> shprec = shapefile.records()
> "
> Then I carry out some edits, and I save my file.
1) Do you close the file?
> When I want to open it again, within the same script, in order to access some of the data I just modified, I get this message :
>
> "Traceback (most recent call last):
>    File "<pyshell#9>", line 1, in<module>
>      troncop=troncon.shapes()
>    File "C:\Python25\Lib\shapefile.py", line 310, in shapes
>      while shp.tell()<  self.shpLength:
>    File "C:\Python25\Lib\shapefile.py", line 222, in __shape
>      print(f.read(8))
2) Did you add that line?  I'm guessing you inserted that right before a 
read() , so you could see what the data looks like.  But this statement 
reads the 8 bytes and prints them, then throws them away.  So the 
original read() that follows will get the next 8 bytes of the file, 
which might not look right.
>    File "C:\Python25\lib\struct.py", line 87, in unpack
>      return o.unpack(s)
> error: unpack requires a string argument of length 8"
>
>
> I reckon this part tries to import header information for shape data, which would be stored in C (?), and fails to "unpack" it in PYTHON.
>
> I've tried putting an "IF" to check whether f.read(8) was actually a string (type) and of length equal to 8. I still get the same error message.
> I've also tried skipping the "unpack" part altogther, not with much success.
>
> I'm really not too familiar with Python, si if anyone could help me out with this, I'd be really grateful.
>
You should supply a link to the pyshp so that people who are willing to 
install it, will be sure to get the same one you did.  Likewise any 
other environmental data about your system.  I can tell you're running 
Python 2.5 on some Windows system, but it'd be nice if you just said so.

Other thoughts:  If there's any binary data in that file, you might need 
to open it with a "b" mode.  Without it Windows will convert crlf into 
linefeeds, which you don't want to do if it's binary data.  We don't see 
your open here, so who knows?


-- 

DaveA



More information about the Tutor mailing list