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

Nate Lastname defensoft at gmail.com
Fri Feb 3 17:15:24 CET 2012


On Fri, Feb 3, 2012 at 10:46 AM, Simeon Tesfaye <
Simeon.Tesfaye at eaudeparis.fr> wrote:

>  Hello everyone,
>
> 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.
> 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))
>   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.
>
> Thanks in advance,
>
> S.T.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
All that this error means is that you have an array longer than the number
of values that you are assigning to it.
I.E:
array = [1, 2, 3]
a, b = array
This causes an error since there are too many values in the array.  To fix
this, just change the second line to:
a, b = array[:2]
in my example.

-- 
My Blog - Defenestration Coding

http://defenestrationcoding.wordpress.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120203/bb884954/attachment.html>


More information about the Tutor mailing list