我正在使用文件数据中的字节数组。我将其打开为'r+b',因此可以更改为二进制文件。
在Python 3.7 docs 中,它解释了 RegExfinditer()可以使用m.start()和m.end()来识别匹配的开始和结束。
在问题Insert bytearray into bytearray Python 中,答案说可以通过使用切片对 bytearray 进行插入。但是当尝试这样做时,会出现以下错误:BufferError: Existing exports of data: object cannot be re-sized.
下面是一个例子:
pat = re.compile(rb'0.?\d* [nN]') # regex, binary "0[.*] n"
with open(file, mode='r+b') as f: # updateable, binary
d = bytearray(f.read()) # read file data as d [as bytes]
it = pat.finditer(d) # find pattern in data as iterable
for match in …Run Code Online (Sandbox Code Playgroud)