如何使用Unix的分离符合令牌? 表明文件是tokenizable使用sed
或xargs
。
有没有办法做相反?
[在:]
some
sentences
are
like
this.
some
sentences
foo
bar
that
[OUT]:
some sentences are like this.
some sentences foo bar that
每一句唯一的分隔符是\n\n
。 我可以做的蟒蛇以下, 但有一个UNIX的办法吗?
def per_section(it):
""" Read a file and yield sections using empty line as delimiter """
section = []
for line in it:
if line.strip('\n'):
section.append(line)
else:
yield ''.join(section)
section = []
# yield any remaining lines as a section too
if section:
yield ''.join(section)
print ["".join(i).replace("\n"," ") for i in per_section(codecs.open('outfile.txt','r','utf8'))]
[出:]
[u'some sentences are like this. ', u'some sentences foo bar that ']