我正在尝试从存储桶中的tarfile中读取wav文件。由于文件很多,我不想先提取这些文件。
相反,我想从tarfile中读取数据并将其流式传输到wavfile.read
(来自scipy.io
)
with tf.gfile.Open(chunk_fp, mode='rb') as f:
with tarfile.open(fileobj=f, mode='r|*') as tar:
for member in ds_text.index.values:
bytes = BytesIO(tar.extractfile(member)) # Obviously not working
rate, wav_data = wavfile.read(bytes)
# Do stuff with data ..
但是,我无法动手wavfile.read
继续工作。
尝试不同的方法会给我带来不同的错误:
tar.extractfile(member).seek(0)
{AttributeError}'_Stream' object has no attribute 'seekable'
tar.extractfile(member).raw.read()
{StreamError}seeking backwards is not allowed
以此类推。
有什么想法可以实现这一目标吗?
答案 0 :(得分:0)
原来,我只是以错误的模式打开了文件。使用r:*
代替r|*
可以做到:
with tarfile.open(fileobj=f, mode='r:*') as tar: