numpy - Python: unziping special files into memory and getting them into a DataFrame -
i'm quite stuck code i'm writing in python, i'm beginner , maybe easy, can't see it. appreciated. thank in advance :)
here problem: have read special data files special extension .fen pandas dataframe.this .fen files inside zipped file .fenx contains .fen file , .cfg configuration file.
in code i've written use zipfile library in order unzip files, , them in dataframe. code following
import zipfile import numpy np import pandas pd def readfenxfile(directory,file): fenxzip = zipfile.zipfile(directory+ '\\' + file, 'r') fenxzip.extractall() fenxzip.close() cfggeneral,cfgdevice,cfgchannels,cfgdtypes=readcfgfile(directory,file[:-5]+'.cfg') #readcfgfile redas .cfg file , returns important data. #here cfgdtypes important contains type of data inside .fen , become column index in final dataframe. if cfgchannels!=none: dtdtype=eval('np.dtype([' + cfgdtypes + '])') dt=np.fromfile(directory+'\\'+file[:-5]+'.fen',dtype=dtdtype) dt=pd.dataframe(dt) else: dt=[] return dt,cfgchannels,cfgdtypes now, extract() method saves unzipped file in hard drive. .fenx files can quite big need of storing (and afterwards deleting them) slow. same now, getting .fen , .cfg files memory, not hard drive.
i have tried things fenxzip.read('whateverthenameofthefileis.fen')and other methods .open() zipfile library. can't .read() returns numpy array in anyway tried.
i know can difficult question answer, because don't have files try , see happens. if have ideas glad of reading them. :) thank much!
here solution found in case can helpful anyone. uses tempfile library create temporal object in memory.
import zipfile import tempfile import numpy np import pandas pd def readfenxfile(directory,file,extractdirectory): fenxzip = zipfile.zipfile(directory+ r'\\' + file, 'r') fenfile=tempfile.spooledtemporaryfile(max_size=10000000000,mode='w+b') fenfile.write(fenxzip.read(file[:-5]+'.fen')) cfggeneral,cfgdevice,cfgchannels,cfgdtypes=readcfgfile(fenxzip,file[:-5]+'.cfg') if cfgchannels!=none: dtdtype=eval('np.dtype([' + cfgdtypes + '])') fenfile.seek(0) dt=np.fromfile(fenfile,dtype=dtdtype) dt=pd.dataframe(dt) else: dt=[] fenfile.close() fenxzip.close() return dt,cfgchannels,cfgdtypes
Comments
Post a Comment