所以我有这个代码
buffer_size = 190000000
start_offset = 0
b = bytearray(buffer_size)
mv = memoryview(b)
end_offset = len(record)
mv[start_offset: end_offset] = record.encode(constants.file_encoding)
每当遇到最后一个语句时,它都会引发如下错误:
ValueError: Memoryview assignment: Lvalue has a different structure than RValue
如果record
是字符串,则字符串中的代码点数len(record)
;您尚未将其编码为字节。bytes
对象的长度是您需要的。此外,赋值end_offset = len(…)
仅在start_offset == 0
时才有意义,并且无需创建memoryview
来分配给bytearray
的切片。
buffer_size = 190000000
b = bytearray(buffer_size)
record_bytes = record.encode(constants.file_encoding)
end_offset = len(record_bytes)
b[:end_offset] = record_bytes