为什么这个二进制比较错在Python? [英] Why this binary comparison wrong in Python?

查看:221
本文介绍了为什么这个二进制比较错在Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读这线程但我没有得到的二进制比较工作。

code

 #!的/ usr / bin中/蟒蛇
进口SYS
进口OS计划生育=打开(/家庭/马嘶/ r.raw,RB)#我认为你不能做这样寻求读取二进制比较
尝试:
    因为aBuf =字节(fp.read(4))
    而aBuf中[3]!= B'\\ X58 \\ x5E':
        因为aBuf =字节(fp.read(4))除:
    打印在位置文件结尾:fp.tell()最后:
    fp.close()

在Debian的Linux的一些输出8.1

在位置

 文件结尾:4

数据 r.raw 是二进制的,其中以十六进制第一行:

  48000000fe5a1eda480000000d00030001000000cd010000010000000000

所以while循环应至少要到位置60。
二进制由处理B'\\ X .. \\ X .. 应该是正确的。

为什么是二进制比较错了?


解决方案

首先,Python 2中的不使用字节() 的。名称字节的别名STR() fp.read()收益 STR 对象重新presenting字节, B preFIX的字符串是一个空操作,它只是为了与Python 3的向前兼容性。

接下来,你是比较3个字符的切片用2字节的字符串:

 >>> LEN('\\ X58 \\ x5E')
2
>>> LEN(12345[3])
3

因此​​比较的永远不会是假的

如果你想测试的前2个字节,使用片2字符,而不是:

 而因为aBuf [2] ='\\ X58 \\ x5E'!

str.startswith()方法:

 ,而不是aBuf.startswith('\\ X58 \\ x5E'):

这并不能说明你的问题,当然,因为你所期望的的全部的进行,因为这样读取文件。<​​/ P>

不过,你可能得到一个的IOError 代替,但您使用的口袋妖怪异常处理的;你抓住他们所有。不要用毯子除外:语句,你正赶上所有可能的错误,包括内存错误

最起码的登录您的例外的:

 尝试:
    因为aBuf = fp.read(4)
    而aBuf中,而不是aBuf.startswith('\\ X58 \\ x5E'):
        因为aBuf = fp.read(4)
除例外情况:
    打印在位置文件结尾:fp.tell()
    进口追踪
    traceback.print_exc()

我已经限制被抓的例外基类例外,至少没有赶上内存错误,键盘中断和发电机退出异常。你真的缩小768,16进一步下降到实际的异常你希望你的code处理(如没有的所有的)。

I am reading this thread but I do not get the binary comparison to work

Code

#!/usr/bin/python
import sys
import os

fp = open("/home/masi/r.raw", "rb")

# I think you cannot do binary comparison with seek so read
try:
    aBuf = bytes(fp.read(4))
    while aBuf[:3] != b'\x58\x5E':
        aBuf = bytes(fp.read(4))

except:
    print "File end at position : ", fp.tell()

finally:
    fp.close()

getting output in Debian Linux 8.1

File end at position :  4

Data r.raw is binary where first line in hex:

48000000fe5a1eda480000000d00030001000000cd010000010000000000

so the while loop should be going at least to the position 60. The handling of binary by b'\x..\x.. should be right.

Why is the binary comparison wrong?

解决方案

First of all, Python 2 doesn't use bytes(). The name bytes is an alias for str(). fp.read() returns str objects representing bytes, and the b prefix for a string literal is a no-op, it is only there for forward compatibility with Python 3.

Next, you are comparing a slice for 3 characters with a 2 byte string:

>>> len('\x58\x5E')
2
>>> len('12345'[:3])
3

so the comparison is never going to be false.

If you wanted to test the first 2 bytes, use a slice for 2 characters instead:

while aBuf[:2] != '\x58\x5E':

or use the str.startswith() method:

while not aBuf.startswith('\x58\x5E'):

This doesn't explain your problem, of course, since you'd expect the whole file to be read because of this.

However, you are probably getting an IOError instead, but you are using Pokemon exception handling; you catching em all. Don't use a blanket except: statement, you are catching all possible errors including memory errors.

At the very least log your exceptions:

try:
    aBuf = fp.read(4)
    while aBuf and not aBuf.startswith('\x58\x5E'):
        aBuf = fp.read(4)
except Exception:
    print "File end at position : ", fp.tell()
    import traceback
    traceback.print_exc()

I've limited the exceptions being caught to the Exception baseclass, to at least not catch memory errors, keyboard interrupts and generator exit exceptions. You really shoud narrow that further down to actual exceptions you want your code to handle (e.g. not everything).

这篇关于为什么这个二进制比较错在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆