[Python] string argument expected, got bytes. in npytar.py

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert .mat files into a tar file, which consists of compressed .npy files. I'm using a modified version of npytar.py from Voxnets. I am using io.BytesIO() as some other people have previously suggested. From what I can gather from the. Does anyone have any solutions?

    This is the code referred to in the traceback of Line 53.

    logging.info('Saving train npy tar file')
    train_records = records['train']
    random.shuffle(train_records)
    write(train_records,'shapenet10_train.tar')


    And here is the code for the traceback at line 19

    def write(records, fname):
    writer = npytar.NpyTarWriter(fname)
    for (classname, instance, rot, fname) in records:
    class_id = int(shapenet10.class_name_to_id[classname])
    name = '{:03d}.{}.{:03d}'.format(class_id, instance, rot)
    arr = scipy.io.loadmat(fname)['instance'].astype(np.uint8)
    arrpad = np.zeros((32,)*3, dtype=np.uint8)
    arrpad[1:-1,1:-1,1:-1] = arr
    writer.add(arrpad, name)
    writer.close()


    import io
    import numpy as np

    ...
    def add(self, arr, name):

    sio = io.BytesIO()
    np.save(sio, arr)
    zbuf = zlib.compress(sio.getvalue())
    sio.close()

    zsio = io.BytesIO(zbuf)
    tinfo = tarfile.TarInfo('{}{}{}'.format(PREFIX, name, SUFFIX))
    tinfo.size = len(zbuf)
    tinfo.mtime = time.time()
    zsio.seek(0)
    self.tfile.addfile(tinfo, zsio)
    zsio.close()
    ...



    Traceback (most recent call last):
    File "C:\Users\yahya\PycharmProjects\Final_Year_Project\convert_shapenet10.py", line 53, in <module>
    write(train_records,'shapenet10_train.tar')
    File "C:\Users\yahya\PycharmProjects\Final_Year_Project\convert_shapenet10.py", line 19, in write
    writer.add(arrpad, name)
    File "C:\Users\yahya\PycharmProjects\Final_Year_Project\npytar.py", line 19, in add
    format.write_array(fid, arr, allow_pickle=allow_pickle,
    File "C:\Users\yahya\PycharmProjects\Final_Year_Project\.venv\Lib\site-packages\numpy\lib\format.py", line 724, in write_array
    _write_array_header(fp, header_data_from_array_1_0(array), version)
    File "C:\Users\yahya\PycharmProjects\Final_Year_Project\.venv\Lib\site-packages\numpy\lib\format.py", line 467, in _write_array_header
    fp.write(header)
    TypeError: string argument expected, got 'bytes'

    Continue reading...

Compartilhe esta Página