Python:如何在压缩文件中加载特定压缩文件的缓冲区对象?

Python:如何在压缩文件中加载特定压缩文件的缓冲区对象?,python,scipy,gzip,tarfile,Python,Scipy,Gzip,Tarfile,我有一个tar.bz2,里面有一个特定的.gz文件列表。在tar.bz2中搜索并找到.gz文件后,我想将这些特定的.gz文件加载到缓冲区。然后我想解压缩那些加载的.gz文件。最后,我想将解压缩后的.gz文件作为.mat文件加载。我希望这样做而不必生成外部文件 注意:.gz压缩文件只是一个.mat文件,而不是压缩文件的进一步列表 因此,为了说明压缩体系结构: big_file.tar.bz2->cat.gz,dog.gz,hello.gz,hello1.gz hello.gz->hello(单个.

我有一个tar.bz2,里面有一个特定的.gz文件列表。在tar.bz2中搜索并找到.gz文件后,我想将这些特定的.gz文件加载到缓冲区。然后我想解压缩那些加载的.gz文件。最后,我想将解压缩后的.gz文件作为.mat文件加载。我希望这样做而不必生成外部文件

注意:.gz压缩文件只是一个.mat文件,而不是压缩文件的进一步列表

因此,为了说明压缩体系结构: big_file.tar.bz2->cat.gz,dog.gz,hello.gz,hello1.gz hello.gz->hello(单个.mat文件)

tar.bz2中匹配的文件名是“hello”,正如标记为“hello.gz”、hello1.gz、。。。等等

我当前的问题是,在tar_bz2.extractfile()之后,我无法正确获取缓冲区格式

snipet的错误出现在loadmat()上,作为对文件问题结尾的搜索,但我确信问题的根源始于tar_bz2.extractfile()之后

以下是我目前掌握的情况:

import tarfile
import gzip
from scipy.io import loadmat

def extract_mat_data(file):
    match_filename = 'hello'
    # Decompress tar.bz2
    mat_file_data = []
    with  tarfile.open(file,'r:bz2',errorlevel=1) as tar_bz2:
        # Loop over all the sub compressed files
        for file_ in tar_bz2:
            # Match only the files with the desired file name.
            if match_filename in file_.name:
                # Extract the match file into a buffer
                file_in_file = tar_bz2.extractfile(file_)
                # Extract the gz file from a buffer
                gz = gzip.GzipFile(fileobj=file_in_file.fileobj,mode='rb')
                # Load the mat format buffer object
                single_mat_data = loadmat(gz)
                # Create a list with the mat data
                mat_file_data.append(single_mat_data)
    return mat_file_data

file = 'big_file.tar.bz2'
extract_mat_data(file)