Python - 生成填充的最有效方法是什么?

时间:2022-10-22 10:43:01

Here's the problem: I'm reading binary files in fairly large blocks (512 KiB) and wish to pad the last block with zeros whenever it is shorter than the block size.

这是问题所在:我正在读取相当大的块(512 KiB)中的二进制文件,并且希望只要它比块大小短,就用零填充最后一个块。

Currently, I'm doing something like this:

目前,我正在做这样的事情:

bytes = f.read(self.chunksize)
if len(bytes) > 0:
    len_diff = self.chunksize - len(bytes)
    if len_diff > 0:
        bytes += reduce(lambda x,y: x+y, ["\0" for i in range(0, len_diff)])

Obviously this is terribly inefficient since that reduce will make a lot of string concatenations. I'm wondering though, how can I achieve this with Python? In C, I'd simply calloc and be done with it.

显然这是非常低效的,因为减少会产生大量的字符串连接。我想知道,我怎样才能用Python实现这一目标?在C中,我只需要calloc并完成它。

If it isn't achievable with Python, I'm willing to convert this code into a C module and/or abandon Python entirely for this project, since it's still on the early stages.

如果使用Python无法实现,我愿意将此代码转换为C模块和/或完全放弃Python用于此项目,因为它仍处于早期阶段。

Cheers!

EDIT: I'm feeling terrible for not remembering to use the * operator. :-)

编辑:我不记得使用*运算符感觉很糟糕。 :-)

This solution worked perfectly for me:

这个解决方案非常适合我:

bytes += "\0" * len_diff

EDIT #2: Using ljust() instead simplified my code a bit, so the correct answer goes to Jeff.

编辑#2:使用ljust()而不是简化我的代码,所以正确的答案归于Jeff。

4 个解决方案

#1


13  

Couldn't you just use ljust() to do the padding since we're dealing with string objects here?

难道你不能只使用ljust()来填充,因为我们在这里处理字符串对象吗?

bytes = f.read(self.chunksize)
if bytes:
    bytes = bytes.ljust(self.chunksize, '\0')

#2


2  

bytes += "\0"*len_diff 

should help

#3


2  

try this.

bytes = "\0" * self.chunksize
rbytes = f.read(self.chunksize)
bytes[:len(rbytes)] = rbytes

or

bytes = f.read(self.chunksize)
bytes += "\0" * (self.chunksize - len(bytes))

#4


2  

How about:

bytes += "\0"*len_diff

#1


13  

Couldn't you just use ljust() to do the padding since we're dealing with string objects here?

难道你不能只使用ljust()来填充,因为我们在这里处理字符串对象吗?

bytes = f.read(self.chunksize)
if bytes:
    bytes = bytes.ljust(self.chunksize, '\0')

#2


2  

bytes += "\0"*len_diff 

should help

#3


2  

try this.

bytes = "\0" * self.chunksize
rbytes = f.read(self.chunksize)
bytes[:len(rbytes)] = rbytes

or

bytes = f.read(self.chunksize)
bytes += "\0" * (self.chunksize - len(bytes))

#4


2  

How about:

bytes += "\0"*len_diff