472,095 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,095 software developers and data experts.

Python equivalent of Perl's $/

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?

Regards, John
--
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)
Aug 20 '07 #1
4 1419
On Aug 19, 1:13 pm, John K Masters <johnmast...@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?

Regards, John
--
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)
Python has a Regular Expressions module. Check it out here:
http://docs.python.org/lib/module-re.html

There's also a chapter from Dive Into Python that covers this topic
too:
http://www.diveintopython.org/regula...ons/index.html

Finally, Python "while" statement's docs can be found here:
http://docs.python.org/ref/while.html

Hope that helps!

Mike

Aug 20 '07 #2

"John K Masters" <jo*********@oxtedonline.netwrote in message
news:ma************************************@python .org...
>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?

Regards, John
--
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)
>>'test\ntest2.\ntest3\ntest4.\ntest5'.split('.\n' )
['test\ntest2', 'test3\ntest4', 'test5']

-Mark T.

Aug 20 '07 #3
John K Masters <jo*********@oxtedonline.netwrote:
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?

Regards, John
Something like this maybe?

import re

input_data = """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?
"""

for para in re.split(r"\.\n", input_data):
print "para = %r" % para

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 20 '07 #4
On Aug 19, 11:13 am, John K Masters <johnmast...@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

Aug 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
699 posts views Thread by mike420 | last post: by
226 posts views Thread by Stephen C. Waterbury | last post: by
1 post views Thread by Edward WIJAYA | last post: by
68 posts views Thread by Lad | last post: by
20 posts views Thread by Xah Lee | last post: by
12 posts views Thread by rurpy | last post: by
10 posts views Thread by Mladen Gogala | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.