How would I write this perl script in python?

Aaron Ginn aaron.ginn at
Mon Nov 5 22:15:06 CET 2001

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 {


In the above script, I subtract 0.775 from the value found in $2 and
replace $2 with the new value ($val).

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.

Aaron J. Ginn                    Phone: 480-814-4463
Motorola SemiCustom Solutions    Pager: 877-586-2318
1300 N. Alma School Rd.          Fax  : 480-814-4058
Chandler, AZ 85224 M/D CH260     mailto:aaron.ginn at

More information about the Python-list mailing list