Python equivalent of Perl's $/
attn.steven.kuo at gmail.com
attn.steven.kuo at gmail.com
Mon Aug 20 15:19:48 EDT 2007
On Aug 19, 11:13 am, John K Masters <johnmast... at oxtedonline.net>
wrote:
> I am currently working my way through Jeffrey Friedl's book Mastering
> Regular Expressions. Great book apart from the fact it uses Perl for the
> examples.
>
> One particular expression that interests me is '$/ = ".\n"' which,
> rather than splitting a file into lines, splits on a period-newline
> boundary. Combined with Perl's 'while (<>)' construct this seems a great
> way to process the files I am interested in.
>
> Without wishing to start a flame war, is there a way to do this in Python?
>
import StringIO
text = """\
To mimic Perl's input record separator in
Python, you can use a generator.
And a substring test.
Perhaps something like the following
is what you wanted.
"""
mockfile = StringIO.StringIO(text)
def genrecords(mockfile, sep=".\n"):
buffer = ""
while True:
while sep in buffer:
idx = buffer.find(sep) + len(sep)
yield buffer[:idx]
buffer = buffer[idx:]
rl = mockfile.readline()
if rl == "":
break
else:
buffer = '%s%s' % (buffer, rl)
yield buffer
raise StopIteration
for record in genrecords(mockfile):
print "READ:", record
--
Hope this helps,
Steven
More information about the Python-list
mailing list