[Tutor] Question on regular expressions

Andrew Robert andrew.arobert at gmail.com
Thu May 25 14:13:28 CEST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Everyone


I did a comparison of the output between the perl and python methodology.

They do basically the same thing but the perl form seems to be more "true"

The python method inserts extra blank lines after each hex value line.

For example:

Original text:

	def handler(signal, frame):
		"""
        	Trap signal interrupts if they occur
		"""

Converted In Perl:

	def handler%28signal%2C frame%29%3A
        	%22%22%22
        	Trap signal interrupts if they occur
        	%22%22%22


Converted In Python:

	def handler%28signal%2C frame%29%3A

        	%22%22%22

        	Trap signal interrupts if they occur

        	%22%22%22

Does anyone know why this might be?

Is the print statement inserting a artificial new line character?

If so, how cam I remove that?


The python code I am using is:



import re,sys

for line i open(r'e:\pycode\sigh.txt','rb'):
    print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)



The file is being opened in rb mode because eventually binary files
would be opened via this method as well.



Alan Gauld wrote:
>> a = open(r'e:\pycode\csums.txt','rb').readlines()
>>
>> for line in a:
>>    print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)
> 
> Or just
> 
> for line in open(r'e:\pycode\csums.txt','rb'):
>   print.....
> 
>> Breaking down the command, you appear to be calling an un-named function
>> to act against any characters trapped by the regular expression.
>>
>> Not familiar with lamda :).
> 
> You ae absolutely right.
> It creates an un-named(or anonymous function). :-)
> 
>> The un-named function does in-place transformation of the character to
>> the established hex value.
> 
> Its actually the call to re.sub() that makes in in place.
> 
>> How would you reverse the process from a python point of view?
> 
> Just write a reverse function for the lamda...
> 
> Alan G.
> 
> 

- --
Thank you,
Andrew Robert
Systems Architect
Information Technologies
MFS Investment Management
Phone:   617-954-5882

E-mail:  arobert at mfs.com
Linux User Number: #201204
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)

iD8DBQFEdZ9oDvn/4H0LjDwRAo89AJwJ64+wpfOnboxw4/+w8PhmZBzgwACfYH7C
VPW5VPyqSWhAUgkoOBorjJM=
=bOj0
-----END PGP SIGNATURE-----


More information about the Tutor mailing list