Presume I have an archive, foo.zip. It contains the parts of a shapefile: foo.shp, foo.dbf, foo.shx, foo.prj, and foo.cpg. I want to work with this shapefile in python, using pyshp, AKA the shapefile library.
Right now, I can extract the archive to disk, then read in the shapefile:
r = shapefile.Reader('foo')
I have quite a few such archives, though, and it would be nice to be able to read in these files without having to first extract each archive. According to pyshp's documentation, you can read in a file-like object with syntax like this:
myshp = open('foo.shp', 'rb') mydbf = open('foo.dbf', 'rb') r = shapefile.Reader(shp=myshp, dbf=mydbf)
And I quote: "This feature is very powerful and allows you to load shapefiles from a url, from a zip file, serialized object, or in some cases a database."
Thus I have tried creating a file-like object from my archive. First, creating the archive:
archive = zipfile.ZipFile('foo.zip')
Works fine. But if I then try
r = shapefile.Reader(archive.open('foo.shp')
I get the error, "shapefile.ShapefileException: Shapefile Reader requires a shapefile or file-like object."
That's legit; I should be passing it a file-like object. So I tried modifying the documentation's canonical example:
myshp = archive.open('foo.shp', 'r') mydbf = archive.open('foo.dbf', 'r') r = shapefile.Reader(shp=myshp, dbf=mydbf)
But this produces the error, "io.UnsupportedOperation: seek".
For what it is worth, I cannot use 'rb' as an option with archive.open(). If I do, I am told, "RuntimeError: open() requires mode 'r', 'U', or 'rU'". Also, using "archive.read()" instead of "archive.open()" just produces the "...requires a shapefile or file-like object" error.
I am stumped. As far as I can tell, I have created archive correctly, I have correctly used archive.open() to grab files as file-like objects, and I have used the correct syntax to have pyshp read from a file-like object. And yet, these errors. Does anyone have any idea what I'm doing wrong here? Has anyone had success passing file-like objects from zip archives to pyshp?