小编rdt*_*tsc的帖子

Python 3.73 插入到 bytearray ="对象不能重新调整大小"

我正在使用文件数据中的字节数组。我将其打开'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)

regex arrays file python-3.x write

5
推荐指数
1
解决办法
249
查看次数

标签 统计

arrays ×1

file ×1

python-3.x ×1

regex ×1

write ×1