Search code examples
pythonpython-3.xdecode

Error when trying to decode bytes to string


Here is my function that to get the binary data.

def get_remote_file_binary_data(self, filename, connection_string = ""):
    file_data = None
    tmp_file_path = copyfile.make_temp_path()
    try:
        if connection_string:
            print("connection string is: ", connection_string) 
            source_file = copyfile.join(connection_string, filename)
        else:
            source_file = filename
    
        copyfile.copyfile(source_file, tmp_file_path)
    
        tmp_file = open(tmp_file_path, "rb")
        file_data = tmp_file.read()
        tmp_file.close()
    finally:
        os.remove(tmp_file_path)
    return file_data

This is my data being returned

b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00
\x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01
\xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"

Here is the code calling the function above.

zip_file = self.get_remote_file_binary_data(zip_filename, connection_str)

Here is what I tried and the errors I got in comments

zip_file = zip_file.decode('UTF-8') # 'utf-8' codec can't decode byte 0x87 in position 12: invalid start byte
zip_file = zip_file.decode('UTF-16')  # codec can't decode bytes in position 54-55: illegal encoding
zip_file = zip_file.decode('ascii')  #'ascii' codec can't decode byte 0x87 in position 12: ordinal not in range(128)
zip_file = zip_file.decode('ANSI_X3.4-1968') # ascii' codec can't decode byte 0x87 in position 12: ordinal not in range(128)

Solution

  • import io
    from zipfile import ZipFile
    
    bites = b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01 \xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"
    
    print(ZipFile(io.BytesIO(bites)).filelist[0])
    

    Output:

    <ZipInfo filename='fake_xml.xml' compress_type=deflate external_attr=0x20 file_size=122 compress_size=114>