perl to python

Michael Coleman mkc at stowers-institute.org
Mon May 10 17:30:18 EDT 2004


Olivier Scalbert <olivier.scalbert at algosyn.com> writes:
> Jarek Zgoda wrote:
>>Olivier Scalbert <olivier.scalbert at algosyn.com> pisze:
>>
>>>What is the python way of doing this :
>>>perl -pi -e 's/string1/string2/' file
>>
>>Use sed.
>>
> yes, but in python ?

Jarek's answer is the correct one, for almost any real situation.

For the purposes of exposition, though, a pythonic equivalent would
be:

    import fileinput

    for l in fileinput.input():
        print l.replace('string1', 'string2')

If you want regular expression substitution and not just constant
strings, use re.sub instead.

Mike

-- 
Mike Coleman, Scientific Programmer, +1 816 926 4419
Stowers Institute for Biomedical Research
1000 E. 50th St., Kansas City, MO  64110



More information about the Python-list mailing list