473,695 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1483
On Aug 19, 1:13 pm, John K Masters <johnmast...@ox tedonline.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*********@ox tedonline.netwr ote in message
news:ma******** *************** *************@p ython.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*********@ox tedonline.netwr ote:
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...@ox tedonline.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.String IO(text)

def genrecords(mock file, sep=".\n"):
buffer = ""
while True:
while sep in buffer:
idx = buffer.find(sep ) + len(sep)
yield buffer[:idx]
buffer = buffer[idx:]
rl = mockfile.readli ne()
if rl == "":
break
else:
buffer = '%s%s' % (buffer, rl)
yield buffer
raise StopIteration

for record in genrecords(mock file):
print "READ:", record
--
Hope this helps,
Steven

Aug 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
6550
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
699
33918
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
226
12577
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
1
2738
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
41
3547
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
68
5854
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
5
2064
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had similar functionality. I am a big fan of Python as an OO language and use it for many system admin utilities, webdev and even MS Excel AddIn development, but I have found that Perl's Template::Extract is so invaluable to my web scraping utilities...
20
4064
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
12
2350
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
10
1402
by: Mladen Gogala | last post by:
I am a Python newbie who decided to see what that Python fuss is all about. Quite frankly, I am a bit perplexed. After having had few months of experience with Perl (started in 1994 with Perl v4, and doing it ever since) , here is what perplexes me: perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;' The equivalent in Python looks like this: Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
0
8616
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8815
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5826
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.