迭代二进制文件 [英] Iterating over a binary file

查看:79
本文介绍了迭代二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅新手问题,但我如何从二进制文件中迭代数据块

(也就是说,我不能只是遍历行,因为

可能根本没有行尾分隔符。基本上我想要这个



f = file(filename,''rb'')

data = f。读(1024)

而len(数据)> 0:

someobj.update(数据)

data = f.read(1024)

f.close()


以上代码有效,但我不喜欢进行两次read()调用。任何

的方法来避免它,或者更优雅的语法?谢谢。

Pardon the newbie question, but how can I iterate over blocks of data
from a binary file (i.e., I can''t just iterate over lines, because
there may be no end-of-line delimiters at all). Essentially I want to
to this:

f = file(filename, ''rb'')
data = f.read(1024)
while len(data) > 0:
someobj.update(data)
data = f.read(1024)
f.close()

The above code works, but I don''t like making two read() calls. Any
way to avoid it, or a more elegant syntax? Thanks.

推荐答案

Derek写道:
Derek wrote:
请原谅新手问题,但我如何迭代块来自二进制文件的数据(即,我不能只是遍历行,因为
可能根本没有行尾分隔符)。基本上我想对此:

f =文件(文件名,''rb'')
数据= f.read(1024)
而len(数据)> 0:
someobj.update(数据)
data = f.read(1024)
f.close()

以上代码有效,但我不知道不喜欢进行两次read()调用。有什么方法可以避免它,或者更优雅的语法?谢谢。
Pardon the newbie question, but how can I iterate over blocks of data
from a binary file (i.e., I can''t just iterate over lines, because
there may be no end-of-line delimiters at all). Essentially I want to
to this:

f = file(filename, ''rb'')
data = f.read(1024)
while len(data) > 0:
someobj.update(data)
data = f.read(1024)
f.close()

The above code works, but I don''t like making two read() calls. Any
way to avoid it, or a more elegant syntax? Thanks.




你可以把生成器中的丑陋收起来:


def blocks(infile,size = 1024):

而True:

block = infile.read(size)

if len(block)== 0:

break

yield block


#use it:

for block(f):

someobj.update(数据)


彼得




You can tuck away the ugliness in a generator:

def blocks(infile, size=1024):
while True:
block = infile.read(size)
if len(block) == 0:
break
yield block

#use it:
for data in blocks(f):
someobj.update(data)

Peter



" Derek" <无** @ none.com>写道:
"Derek" <no**@none.com> writes:
f = file(filename,''rb'')
data = f.read(1024)
而len(data)> 0:
someobj.update(数据)
data = f.read(1024)
f.close()

以上代码有效,但我不知道不喜欢进行两次read()调用。有什么方法可以避免它,或者更优雅的语法?谢谢。
f = file(filename, ''rb'')
data = f.read(1024)
while len(data) > 0:
someobj.update(data)
data = f.read(1024)
f.close()

The above code works, but I don''t like making two read() calls. Any
way to avoid it, or a more elegant syntax? Thanks.




你可以使它变得更加丑陋:


f = file(filename,''rb'')

而1:

data = f.read(1024)

if len(data)< = 0:

破解

someobj.update(数据)

f.close()


有提议要添加赋值表达式运算符

就像在C中一样,所以你可以说类似


f = file(filename,''rb'')

而len(数据:= f.read(1024))> 0:

someobj.update(数据)

f.close()


但这是圣洁的主题在这里战争太多次了;-)。不要这么做是为了等待它。



You can make it even uglier:

f = file(filename, ''rb'')
while 1:
data = f.read(1024)
if len(data) <= 0:
break
someobj.update(data)
f.close()

There''s been proposals around to add an assignment-expression operator
like in C, so you could say something like

f = file(filename, ''rb'')
while len(data := f.read(1024)) > 0:
someobj.update(data)
f.close()

but that''s the subject of holy war around here too many times ;-). Don''t
hold your breath waiting for it.


Paul Rubin< http:// ph **** @ NOSPAM。无效>写道:
Paul Rubin <http://ph****@NOSPAM.invalid> writes:
上面的代码有效,但我不喜欢进行两次read()调用。有什么方法可以避免它,或者更优雅的语法?谢谢。
你可以使它变得更加丑陋:

f =文件(文件名,''rb'')
而1:
data = f.read( 1024)
如果len(数据)< = 0:
打破
someobj.update(数据)
f.close()

那里'有人提议在C中添加一个赋值表达式运算符,所以你可以说像

f = file(filename,''rb'')
而len(数据:= f.read(1024))> 0:
someobj.update(数据)
f.close()
The above code works, but I don''t like making two read() calls. Any
way to avoid it, or a more elegant syntax? Thanks.
You can make it even uglier:

f = file(filename, ''rb'')
while 1:
data = f.read(1024)
if len(data) <= 0:
break
someobj.update(data)
f.close()

There''s been proposals around to add an assignment-expression operator
like in C, so you could say something like

f = file(filename, ''rb'')
while len(data := f.read(1024)) > 0:
someobj.update(data)
f.close()




这很有趣,但我发现第一个版本很多比

第二个更可读。特别是如果我有意识地忘记了做很多东西

条件的一部分时间来自C的灌输。如果有很多

的东西,你必须再多看一眼,它会变成

惯用,你学到的东西,也许甚至是食谱的东西,而不是显而易见的b $ b。

但是这是太多次在这里发生圣战的主题;-)。不要屏住呼吸等待它。



It''s funny, but I find the first version much more readable than the
second one. Especially if I consciously forget the "do lots of stuff
in condition part of while" indoctrination from C. If there is lots of
stuff in while you have to stare at it a bit more, and it becomes
"idiomatic", something you learn, perhaps even cookbook stuff, instead
of obvious-as-such.
but that''s the subject of holy war around here too many times ;-). Don''t
hold your breath waiting for it.




可能是真的。而不是:=,我不介意整体上摆脱

表达式/语句差异。


-

Ville Vainio http://www.students.tut.fi / ~vainio24


这篇关于迭代二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆