How would I write this perl script in python?

Nick Mathewson QnickQm at alum.mit.edu
Mon Nov 5 18:45:50 EST 2001


In article <snofmgq5r9.fsf at motorola.com>, Aaron Ginn wrote:
> 
> I rarely use Perl except in cases where it is not obvious how to do
> something in Python that I already know how to do in Perl.  I'm curious
> what the best way to rewrite the following Perl script in Python is.
> 
> --------------------------------------------------
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;
> 
> my $val = 0;
> 
> while (<>) {
>   if (/^(\w+\s+)(\-*\d+\.\d+)(\s+)(\-*\d+\.\d+)$/) {
>     $val = $2 - 0.775;
>     printf "%s%s%s%s\n", $1, $val, $3, $4;
>   }
>   else {
>     print;
>   }
> 
> }

#!/usr/bin/python
#
# Untested and maybe buggy; use at your own risk. 
import fileinput, re

# Note: this pattern is overly verbose; you don't need to escape a
# '-' character.
pattern = re.compile(r'(\w+\s+)(\-*\d+\.\d+)(\s+)(\-*\d+\.\d+)')

for line in fileinput.input():
    m = pattern.match(line)
    if m:
        val = float(m.group(2)) - 0.775
        print "%s%s%s%s" % (m.group(1), val, m.group(3), m.group(4))
    else:
        print line
 
 [...]
> I'm not sure how to isolate the different parts of the regexp in
> Python as I have done in Perl by using parentheses and then referring
> to the parts as $1, $2, $3, etc.  I've used Python's re module many
> times, but mostly only for searching.  It seems I always revert to
> Perl when I have a complicated (or not so complicated) search and
> replace task to perform.

I recommend that you check out the section of the documentation for the
re module that talks about match objects.  Briefly, the re.match and
re.search functions return an object that contains (among other things)
the sections of the string matching the parenthesized groups in your
regexp.  You may access them one-at-a-time by calling m.group(grpnumber) or
m.group(groupname); or you can get a list of them by calling m.groups().

Yrs,

-- 
 Nick Matthewsen    <Q nick Q m at alum dot mit dot edu> 
 aa 1011011011       Remove Q's to respond.  No spam.



More information about the Python-list mailing list