0

读取字节会产生相互矛盾的结果

    bytes_file = BytesIO(requests.get(source_url).content)

    accepted_start_bytes = {
    "jpeg": b'\xFF\xD8\xFF',
    "png":  b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
    }


    print( bytes_file.read().startswith(accepted_start_bytes['jpeg']))

    print( [bytes_file.read().startswith(accepted_start_bytes['jpeg'])])

有输出

>>> True
>>> False

我想这些应该是一样的......

4

1 回答 1

0

我找到了解决方案。事实证明,由于文件的读取方式,您需要在读取文件之前寻找文件的零位。

代码应该是

bytes_file = BytesIO(requests.get(source_url).content)

accepted_start_bytes = {
"jpeg": b'\xFF\xD8\xFF',
"png":  b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
}

bytes_file.seek(0)
print( bytes_file.read().startswith(accepted_start_bytes['jpeg']))
bytes_file.seek(0)
print( [bytes_file.read().startswith(accepted_start_bytes['jpeg'])])

这得到了结果

>>> True
>>> True
于 2018-10-12T10:20:09.450 回答