输入记录分隔符(相当于perl的“$ |”) [英] input record seperator (equivalent of "$|" of perl)
问题描述
我知道我可以从文件对象中执行readline()。
但是,我怎么能读到特定的分隔符?
for exmple,
如果我的文件是
名称
职业
id
#
name2
profession3
id2
我会喜欢将这个文件作为记录阅读。
我可以通过定义一个记录分隔符在perl中执行此操作;
在python中是否存在等效文件?
谢谢
Hi,
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python?
thanks
推荐答案
le *******@yahoo.com 写道:
le*******@yahoo.com wrote:
我知道我可以从文件对象做readline()。
但是,如何我可以读到一个特定的分隔符吗?
例如,如果我的文件是
名称
专业
id
#
name2
profession3
id2
我想将此文件作为记录阅读。
我可以在perl中执行此操作通过定义一个记录分隔符;
在python中有一个等价物吗?
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python?
不是真的;你必须手动完成。
如果文件不是太大,请考虑阅读所有文件,并拆分
分隔符:
用于file.read()中的记录。分割(分隔符):
打印记录#进程记录
如果您使用的是面向行的分隔符,就像在您的示例中那样,
itertools.groupby等工具可以非常方便:
来自itertools import groupby
def is_separator(line):
返回行[:1] =="#"
表示sep,记录为groupby(文件,is_separator):
如果不是sep:
打印列表(记录)#process record
或者你可以拼出来的东西:
record = []
for line in file:
if line [0] =="#":
if record:
print record #process record
record = []
else:
record.append(line)
如果记录:
print r ecord#处理最后一条记录,如果有的话
< / F>
not really; you have to do it manually.
if the file isn''t too large, consider reading all of it, and splitting on the
separator:
for record in file.read().split(separator):
print record # process record
if you''re using a line-oriented separator, like in your example, tools like
itertools.groupby can be quite handy:
from itertools import groupby
def is_separator(line):
return line[:1] == "#"
for sep, record in groupby(file, is_separator):
if not sep:
print list(record) # process record
or you could just spell things out:
record = []
for line in file:
if line[0] == "#":
if record:
print record # process record
record = []
else:
record.append(line)
if record:
print record # process the last record, if any
</F>
le ******* @ yahoo.com 写道:
我知道我可以从文件对象做readline()。
但是,我怎么能读到一个特定的分隔符?
例如,
如果我的文件是
名字
专业
id
#
name2
professional3
id2
我想把这个文件作为记录阅读。
我可以通过定义一个记录分隔符在perl中执行此操作;
在python中是否有相应的内容?
感谢
Hi,
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python?
thanks
我不喜欢不这么认为。但是在pyNMS包中
( http:// sourceforge / net / projects / pynms )有一个名为
" expect"的模块,以及一个类Expect。有了它你可以包装一个文件对象和
使用Expect.read_until()方法做你想要的。
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~
Keith Dart< kd *** @ kdart.com>
公钥:ID:F3D288E4
================================= ================= ===================
I don''t think so. But in the pyNMS package
(http://sourceforge/net/projects/pynms) there is a module called
"expect", and a class "Expect". With that you can wrap a file object and
use the Expect.read_until() method to do what you want.
--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4
================================================== ===================
le*******@yahoo.com 写道:
le*******@yahoo.com wrote:
我知道我可以从文件对象做readline()。
但是,我怎么能读到特定的分隔符?
例如,
如果我的话文件是
名称
专业
id
#
name2
profession3
id2
我我希望将这个文件作为记录阅读。
我可以通过定义一个记录分隔符在perl中执行此操作;
在python中是否有相应的内容?
感谢
Hi,
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python?
thanks
实际回答你的问题,没有相当于
To actually answer your question, there is no equivalent to
这篇关于输入记录分隔符(相当于perl的“$ |”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!