Python equivalent of Perl's $/
John K Masters
johnmasters at oxtedonline.net
Mon Aug 20 16:02:44 EDT 2007
On 19:19 Mon 20 Aug , attn.steven.kuo at gmail.com wrote:
>
> 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
Thanks, this also looks like a good way to go but ATM beyond my level of
Python knowledge. I've not reached the generator chapter yet but I'll
flag the message and return later.
Regards, John
--
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)
More information about the Python-list
mailing list