2013-07-08 56 views
0

我读了JPEG字符串转换为字节为String

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

firstTwoBytes INY我的调试器的前几个字节:字节:B '\ XFF \ XD8',这是正确的?

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

感谢

回答

2

试试这个:

if firstTwoBytes != b'\xff\xd8': 
+0

陈感谢。有趣的是这是一个python 3的东西? python2和python3在行为上有什么不同吗? –

+1

有一个区别:Python 3中'string'是'unicode' – ElmoVanKielmo

1

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

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