简体   繁体   English

“utf-8”编解码器无法解码 position 中的字节 0x8b 0:无效的起始字节 django

[英]'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte django

so i have variable contains with bytes and i want to write it to text and download it.所以我有包含字节的变量,我想将它写入文本并下载它。 but to use write, it's must in bytes.但要使用写入,它必须以字节为单位。 so how to make from bytes to string?那么如何从字节到字符串呢?

now i got error like this.现在我得到了这样的错误。 i tried to decode it, but it cannot works.我试图解码它,但它无法工作。

'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte “utf-8”编解码器无法解码 position 中的字节 0x8b 0:起始字节无效

here's the code:这是代码:

def create_file(f):
    print(f) #f = b'\x8b\x86pJ'
    response = HttpResponse(content_type="text/plain")
    response['Content-Disposition'] = 'attachment; filename=file.txt'

    filename = f
    print(filename) # filename = b'\x8b\x86pJ'
    download = filename.decode('utf-8')
    response.write(download)
    print(response)

    return response

You can try你可以试试

def create_file(f):
    print(f) #f = b'\x8b\x86pJ'
    response = HttpResponse(content_type="text/plain")
    response['Content-Disposition'] = 'attachment; filename=file.txt'

    filename = f
    print(filename) # filename = b'\x8b\x86pJ'
    download = filename.decode('latin-1')
    response.write(download)
    print(response)

    return response

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeDecodeError:“utf-8”编解码器无法解码位置 1 的字节 0x8b:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte` - `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte` “utf-8”编解码器无法解码 position 中的字节 0x8b 1:无效的起始字节 - 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 1 的字节 0x8b:无效的起始字节,同时在 Pandas 中读取 csv 文件 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte, while reading csv file in pandas UnicodeDecodeError:'utf-8'编解码器无法解码位置1的字节0x8b - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1 (django) 'utf-8' 编解码器无法解码位置 0 的字节 0x89:无效的起始字节 - (django) 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0x80:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte “utf-8”编解码器无法解码位置 928 中的字节 0x93:起始字节无效 - 'utf-8' codec can't decode byte 0x93 in position 928: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 1072 中的字节 0x95:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 1072: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 3131 中的字节 0x80:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM