perl to python

Pete Forman pete.forman at westerngeco.com
Tue May 11 10:10:27 EDT 2004


Jason Mobarak <jmob at spam__unm.edu> writes:
 > John Roth wrote:
 > > "Olivier Scalbert" <olivier.scalbert at algosyn.com> wrote in message
 > > news:409e86e9$0$22811$a0ced6e1 at news.skynet.be...
 > > > What is the python way of doing this :
 > > > perl -pi -e 's/string1/string2/' file
 > >
 > > I'm not sure what the -pi and -e switches do, but the rest is
 > > fairly simple, although not as simple as the perl one-liner.
 > > Just load the file into a string variable, and either use the
 > > string .replace() method, or use a regx, depending on which is
 > > appropriate. Then write it back out.
 > > [...]
 > 
 > More obfuscated:
 > 
 > python -c '(lambda fp: fp.write(fp.seek(0) or
 > "".join([L.replace("th","ht") for L in fp])))(file("foo","rw+"))'

For a less obfuscated approach, look at PyOne to run short python
scripts from a one-line command.

  http://www.unixuser.org/~euske/pyone/

-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at westerngeco.com    -./\.-   opinion of Schlumberger, Baker
http://petef.port5.com           -./\.-   Hughes or their divisions.



More information about the Python-list mailing list