I am trying to load a DBF file that is contained within a zip file into a variable without extracting the zip file. I have tried using the following code, but it is not working:
import zipfile
from dbfread import DBF
def load_dbf_from_zip(zip_filename):
with zipfile.ZipFile(zip_filename, 'r') as z:
dbf_filename = z.namelist()[0]
with z.open(dbf_filename) as f:
dbf = DBF(f)
return dbf
dbf = load_dbf_from_zip('my_zip_file.zip')
This code is not working because it is trying to read the DBF file from the zip file using the DBF constructor, which expects a file object as input. However, the open() method of the ZipFile class returns a ZipExtFile object, which is not a file object.
How can I load the DBF file into a variable without extracting the zip file?
It looks like you can't. The library you are using expects a filename as first argument to
DBS(), then tries to open and read the file itself. Moreover, theDBSclass seems to open and read the file multiple times in internal functions, so you can't just create a temporary file and delete it after instantiatingDBS(filename). Pretty silly unfortunately.You will have to either extract the file to disk or create a
NamedTemporaryFile, write data to it and pass its name toDBS. That file should lives as long as theDBSclass you work with though.Overall, I would say this library offers a pretty poor interface, the underlying file could even change and the library will re-open it after it has changed, which I am not sure is something one would normally expect.