Looking for Python equivalent to a Perl'ism 
Author Message
 Looking for Python equivalent to a Perl'ism

I am trying to figure out how to do the python equivalent of the
following perl code, which finds all the groups tagged by 'xx' and
replaces them with text looked up in a dictionary:
----------
my %dict;
my $string="this is xxfooxx this is junk xxmumblexx and more stuff";
%dict=(foo=>"yechfoo",mumble=>"yukkomumble");

$string =~ s/xx(\S+)xx/$dict{$1}/g;

print $string;
---------------------

My Python equivalent without the backreference dictionary lookup is:

dict ={'xxx':'foo',
       'yyy':'barf',
       }
p=re.compile(r'zz(\w+)zz')
s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
print re.sub(p,'junk',s)

--------------

But I do not understand how to include backreferences into the regex
substitution as is conveniently done in perl.  Can someone tell me if
there is an easy Python solution to this?

Thanks in advance.

David Lees



Tue, 16 Sep 2003 01:49:27 GMT  
 Looking for Python equivalent to a Perl'ism
Quote:

> I am trying to figure out how to do the Python equivalent of the
> following perl code, which finds all the groups tagged by 'xx' and
> replaces them with text looked up in a dictionary:
> ----------
> my %dict;
> my $string="this is xxfooxx this is junk xxmumblexx and more stuff";
> %dict=(foo=>"yechfoo",mumble=>"yukkomumble");

> $string =~ s/xx(\S+)xx/$dict{$1}/g;

> print $string;
> ---------------------

> My Python equivalent without the backreference dictionary lookup is:

> dict ={'xxx':'foo',
>        'yyy':'barf',
>        }
> p=re.compile(r'zz(\w+)zz')
> s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
> print re.sub(p,'junk',s)

How about a non re solution.

import string
s = string.split(s, "zz")
for r in range(len(s)):
    try:
        s[r] = dict[s[r]]
    except KeyError:
        pass
s = string.join(s, "")

Roland Schlenker



Tue, 16 Sep 2003 04:20:14 GMT  
 Looking for Python equivalent to a Perl'ism

Quote:

> ----------
> my %dict;
> my $string="this is xxfooxx this is junk xxmumblexx and more stuff";
> %dict=(foo=>"yechfoo",mumble=>"yukkomumble");

> $string =~ s/xx(\S+)xx/$dict{$1}/g;

> print $string;
> ------------

To simply substitute the contents of a single group, you can
include \N, or \g<N>:

p=re.compile(r'zz(\w+)zz')
s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
print p.sub('\g<1>', s)

This produces "this is and so is xxx abc that an yyy abc xxx or else"
You want to do a dictionary lookup, though; instead of passing a
replacement string, you can pass a function which will be handed the
match object, and can return whatever string it likes:

def sub_func(match):
    return dict[ match.group(1) ]

print p.sub(sub_func, s)

This will output "this is and so is foo abc that an barf abc foo or
else".

Note that this raises an exception if the contents of group 1 aren't
found in the dictionary, so 'zzrandomzz' will trigger a KeyError.
Perl would probably just assume undef or the empty string or something
like that, so sub_func() would have to be changed accordingly --
to dict.get(match.group[1], ""), perhaps.

--amk



Tue, 16 Sep 2003 04:43:42 GMT  
 Looking for Python equivalent to a Perl'ism
While this is not exactly what you want, were you aware of the following
syntax:

dict = {"foo":"yechfoo", "mumble":"yukkomumble"}

print "This is %(foo)s this is junk %(mumble)s and more stuff" % dict

--Stephen
(replace 'NOSPAM' with 'seraph' to respond)

Quote:

>I am trying to figure out how to do the Python equivalent of the
>following perl code, which finds all the groups tagged by 'xx' and
>replaces them with text looked up in a dictionary:
>----------
>my %dict;
>my $string="this is xxfooxx this is junk xxmumblexx and more stuff";
>%dict=(foo=>"yechfoo",mumble=>"yukkomumble");

>$string =~ s/xx(\S+)xx/$dict{$1}/g;

>print $string;
>---------------------

>My Python equivalent without the backreference dictionary lookup is:

>dict ={'xxx':'foo',
>       'yyy':'barf',
>       }
>p=re.compile(r'zz(\w+)zz')
>s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
>print re.sub(p,'junk',s)

>--------------

>But I do not understand how to include backreferences into the regex
>substitution as is conveniently done in perl.  Can someone tell me if
>there is an easy Python solution to this?

>Thanks in advance.

>David Lees

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----


Tue, 16 Sep 2003 04:45:26 GMT  
 Looking for Python equivalent to a Perl'ism
Thanks for the solution.  Also, this is a big help to my understanding
the 'sub' section of the documentation at:
http://www.python.org/doc/current/lib/Contents_of_Module_re.html

david lees

<snip>

Quote:

> To simply substitute the contents of a single group, you can
> include \N, or \g<N>:

> p=re.compile(r'zz(\w+)zz')
> s='this is and so is zzxxxzz abc that an zzyyyzz abc zzxxxzz or else'
> print p.sub('\g<1>', s)

> This produces "this is and so is xxx abc that an yyy abc xxx or else"
> You want to do a dictionary lookup, though; instead of passing a
> replacement string, you can pass a function which will be handed the
> match object, and can return whatever string it likes:

> def sub_func(match):
>     return dict[ match.group(1) ]

> print p.sub(sub_func, s)

<snip>


Tue, 16 Sep 2003 13:27:36 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Python equivalent of Perl's assignment to $0

2. Python Equivalent of Perl's Safe module?

3. 2nd Request: Looking for Perl module equivalent

4. Ruby equivalent to perl's...

5. Equivalent of Perl's use locale

6. Ruby equivalent of Perl's h2ph?

7. Equivalent to Perl's /g and pos()?

8. Equivalent to Perl's Data::Dumper

9. Equivalent to Perl's pack statement?

10. Tcl equivalent for Perl's Win32::OLE, LWP::UserAgent, HTTP::Request, Net::Telnet

11. Tcl equivalent to Perl's format command?

12. Python equivalent of two Perl statements

 

 
Powered by phpBB® Forum Software