处理零分隔线的典型方法? [英] Canonical way of dealing with null-separated lines?

查看:68
本文介绍了处理零分隔线的典型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种规范的方法来迭代文件的行,

是否为空分隔而不是换行符?当然,我可以使用read()和split()等来实现我自己的迭代器,但是考虑到使用find -print0,它可以实现
。是如此常见,似乎

应该有一个更加规范的方式。


|> oug

Is there a canonical way of iterating over the lines of a file that
are null-separated rather than newline-separated? Sure, I can
implement my own iterator using read() and split(), etc., but
considering that using "find -print0" is so common, it seems like
there should be a more cannonical way.

|>oug

推荐答案

2005年2月23日星期三下午10:54:50 -0500,道格拉斯艾伦写道:
On Wed, Feb 23, 2005 at 10:54:50PM -0500, Douglas Alan wrote:
是否有规范的迭代方式
是否以空行分隔的文件?
Is there a canonical way of iterating over the lines of a file that
are null-separated rather than newline-separated?




我不确定是否有规范方法,但我建议使用一个

生成器得到这样的东西,其中''f''是一个文件对象:


def readnullsep(f):

#需要一个地方放置一个空分隔字符串的潜在部分

#跨越缓冲区边界

retain = []


而True:

instr = f.read(2048)

if len(instr)== 0:

#文件结束

休息


#拆分空值

splitstr = instr.split(''\ 0'')


#与上一次阅读遗留的任何内容合并

retain.append(splitstr [0])

splitstr [0] =''''。join(retain)


#保留下一个循环的最后一个部分并产生剩余部分

retain = [splitstr [-1]]

for splitstr中的元素[:-1]:

收益元素


#收益余额

收益率保留[0]


Chris



I''m not sure if there is a canonical method, but I would recommending using a
generator to get something like this, where ''f'' is a file object:

def readnullsep(f):
# Need a place to put potential pieces of a null separated string
# across buffer boundaries
retain = []

while True:
instr = f.read(2048)
if len(instr)==0:
# End of file
break

# Split over nulls
splitstr = instr.split(''\0'')

# Combine with anything left over from previous read
retain.append(splitstr[0])
splitstr[0] = ''''.join(retain)

# Keep last piece for next loop and yield the rest
retain = [splitstr[-1]]
for element in splitstr[:-1]:
yield element

# yield anything left over
yield retain[0]

Chris


Douglas Alan写道:
Douglas Alan wrote:
是否有规范的方法迭代文件的行
是否为空分隔而不是换行符?当然,我可以使用read()和split()等来实现我自己的迭代器,但是考虑到使用find -print0,是如此常见,似乎应该有一个更加规范的方式。
Is there a canonical way of iterating over the lines of a file that
are null-separated rather than newline-separated? Sure, I can
implement my own iterator using read() and split(), etc., but
considering that using "find -print0" is so common, it seems like
there should be a more cannonical way.




你可以从这个代码开始并添加''\'''作为行终止者:

http: //members.dsl-only.net/~daniels/ilines.html

--Scott David Daniels
Sc *********** @ Acm.Org


Christopher De Vries <德***** @ idolstarastronomer.com>写道:
Christopher De Vries <de*****@idolstarastronomer.com> writes:
我不确定是否有规范的方法,但我会建议使用生成器来获得这样的东西,其中''f''
是一个文件对象:
I''m not sure if there is a canonical method, but I would
recommending using a generator to get something like this, where ''f''
is a file object:




感谢生成器。当它与find -print0一起使用时,它会在结尾处返回一个额外的空行

,这可能并不理想,并且

也不是正常文件行的方式迭代器的行为。但不要担心

- 我可以解决它。


在任何情况下,作为建议任何人安排

要放入标准库的东西,应该有像这样的东西

,所以每个人都不需要重新发明轮子(即使是

,如果它是一个容易重新发明的轮子),对于任何系统管理员来说,b $ b(以及许多其他用户)几乎每天都想做的事情。


|> oug



Thanks for the generator. It returns an extra blank line at the end
when used with "find -print0", which is probably not ideal, and is
also not how the normal file line iterator behaves. But don''t worry
-- I can fix it.

In any case, as a suggestion to the whomever it is that arranges for
stuff to be put into the standard library, there should be something
like this there, so everyone doesn''t have to reinvent the wheel (even
if it''s an easy wheel to reinvent) for something that any sysadmin
(and many other users) would want to do on practically a daily basis.

|>oug


这篇关于处理零分隔线的典型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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