0

我读了 JPEG 的前几个字节

f = open(filename, 'rb')
firstTwoBytes = f.read(2)
if firstTwoBytes != '\xff\xd8':

我的调试器中的 firstTwoBytes 是: bytes: b'\xff\xd8' 哪个是正确的?

所以我的字符串比较失败了。如何最好地解决这个问题?

谢谢

4

2 回答 2

2

尝试这个:

if firstTwoBytes != b'\xff\xd8':
于 2013-07-08T10:02:57.297 回答
1

所以,比较二进制而不是字符串:

f = open(filename, 'rb')
firstTwoBytes = f.read(2)
if firstTwoBytes != b'\xff\xd8':
于 2013-07-08T10:02:52.283 回答