读取字节会产生冲突的结果
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
我认为这些应该是相同的...
答案 0 :(得分: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