简体   繁体   English

字节数组中的Python3搜索

[英]Python3 searching in bytearray

I have this strange problem with using find()/index() (don't know if there is any difference between them) with bytesarray. 我在使用bytesarray的find()/ index()(不知道它们之间是否有任何区别)时遇到了一个奇怪的问题。

I'm working with binary file, I have loaded it as bytesarray and now I need to find tokens that indicate beginning of message and end of message. 我正在使用二进制文件,已将其加载为bytesarray,现在需要查找指示消息开头和消息结尾的标记。 Everything works fine with finding the beginning of message (0x03 0x02) but I keep getting the same position where I started searching when I search for the end (0x00) 找到消息的开头(0x03 0x02)一切正常,但是在搜索结尾(0x00)时,我一直保持原位置

    msg_start_token = bytearray((int(0x03), int(0x02)))
    msg_end_token = bytes(int(0x00))

    def get_message(file,start_pos):
        msg_start = file.find(msg_start_token,start_pos) + 2
        print(hex(msg_start))
        msg_end = file.find(msg_end_token,msg_start)
        print(hex(msg_end))
        msg = file[msg_start:msg_end]
        print(msg)
        return (msg, msg_end)  

I haven't really worked with binary files before so I don't know maybe I'm missing something really simple in fact. 之前我还没有真正使用过二进制文件,所以我不知道也许我实际上缺少了一些非常简单的东西。

You need to start searching at the next position , so search at: 您需要在下一个位置开始搜索,因此请在以下位置搜索:

file.find(msg_start_token, start_pos + 1)

because the search starts at start_pos , and if msg_start_token is found at that position, find will return start_pos of course. 因为搜索开始start_pos ,如果msg_start_token在那个位置找到,找到会返回start_pos当然。

As for the difference between .index() and .find() ; 至于.index().find()之间的区别; .index() raises a ValueError exception if the substring is not found, while .find() returns -1 instead. 如果未找到子字符串,则.index()引发ValueError异常,而.find()返回-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM