Olivier Scalbert <olivier.scalbe rt@algosyn.com> pisze:
[color=blue]
> What is the python way of doing this :
> perl -pi -e 's/string1/string2/' file[/color]
Olivier Scalbert <olivier.scalbe rt@algosyn.com> pisze:
[color=blue][color=green][color=darkred]
>>>What is the python way of doing this :
>>>perl -pi -e 's/string1/string2/' file[/color]
>>
>>Use sed.[/color]
>
> yes, but in python ?[/color]
Are you paid for doing everything in Python? This problem is much easier
to sort out by other means.
But of course, it is possible. I'm pretty sure you will get such
solution here.
Olivier Scalbert wrote:
[color=blue]
> Jarek Zgoda wrote:
>[color=green]
>> Olivier Scalbert <olivier.scalbe rt@algosyn.com> pisze:
>>
>>
>>[color=darkred]
>>> What is the python way of doing this :
>>> perl -pi -e 's/string1/string2/' file
>>>[/color]
>>
>>
>> Use sed.
>>
>>
>>[/color]
> yes, but in python ?[/color]
I wonder what the motivation behind your question is?
Do you have Python and not Perl or sed available?
Is the request from part of a larger conversion task?
Do you just want to compare the Perl to the Python solution?
"Olivier Scalbert" <olivier.scalbe rt@algosyn.com> wrote in message
news:409e86e9$0 $22811$a0ced6e1 @news.skynet.be ...[color=blue]
> Hello ,
>
> What is the python way of doing this :
> perl -pi -e 's/string1/string2/' file
> ?
> Thanks
> Olivier[/color]
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.
from the python prompt (not the command prompt)
that's something like: (untested)
var = open("file", "r").read().rep lace("string1", "string2")
open("file", "w").write( var)
I think this is about as obfusticated as you can get -
you'll lose the file if you try for a one-liner.
John Roth wrote:
[color=blue]
>"Olivier Scalbert" <olivier.scalbe rt@algosyn.com> wrote in message
>news:409e86e9$ 0$22811$a0ced6e 1@news.skynet.b e...
>
>[color=green]
>>Hello ,
>>
>>What is the python way of doing this :
>>perl -pi -e 's/string1/string2/' file
>>?
>>Thanks
>>Olivier
>>
>>[/color]
>
>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.
>
>from the python prompt (not the command prompt)
>that's something like: (untested)
>
>var = open("file", "r").read().rep lace("string1", "string2")
>open("file", "w").write( var)
>
>I think this is about as obfusticated as you can get -
>you'll lose the file if you try for a one-liner.
>
>John Roth
>
>
>
>[/color]
Thx John !
John Roth wrote:[color=blue]
> "Olivier Scalbert" <olivier.scalbe rt@algosyn.com> wrote in message
> news:409e86e9$0 $22811$a0ced6e1 @news.skynet.be ...
>[color=green]
>>Hello ,
>>
>>What is the python way of doing this :
>>perl -pi -e 's/string1/string2/' file
>>?
>>Thanks
>>Olivier[/color]
>
>
> 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.
>
> from the python prompt (not the command prompt)
> that's something like: (untested)
>
> var = open("file", "r").read().rep lace("string1", "string2")
> open("file", "w").write( var)
>
> I think this is about as obfusticated as you can get -
> you'll lose the file if you try for a one-liner.
>
> John Roth
>[/color]
More obfuscated:
python -c '(lambda fp: fp.write(fp.see k(0) or
"".join([L.replace("th", "ht") for L in fp])))(file("foo", "rw+"))'
>>>>> "Michael" == Michael Coleman <mkc@stowers-institute.org> writes:
Michael> Olivier Scalbert <olivier.scalbe rt@algosyn.com> writes:[color=blue][color=green]
>> Jarek Zgoda wrote:[/color][/color]
[color=blue][color=green][color=darkred]
>>> Use sed.
>>>[/color]
>> yes, but in python ?[/color][/color]
Michael> Jarek's answer is the correct one, for almost any real
Michael> situation.
Not really. Using Python is more portable, and doesn't introduce a new
dependency. And if it's trivial in Python, why introduce yet another
dependency?
On 2004-05-09, Olivier Scalbert <olivier.scalbe rt@algosyn.com> wrote:[color=blue]
> Hello ,
>
> What is the python way of doing this :
> perl -pi -e 's/string1/string2/' file
> ?[/color]
To expand on what others have said, python emphasizes readability over
compactness and obscure shortcuts. The perl "-pi" idiom wraps everything
around a nice ammount of code, and the "-e" idiom wraps some more code.
a script that sort of has some of the same functionality would go
something like this:
#############st art bad code########### #######
#!/usr/local/bin/python
import getopt,sys,os,r e
#get your command line options
#files will be in
optlist, args = getopt.getopt(s ys.argv[1:],'e:')
#do the -p loop.
for filename in args:
#do the -i "in place" edit.
oldfilename = filename+'.bak'
os.rename(filen ame,oldfilename )
newfile = open(filename,' w')
#continue the -p loop
for line in open(oldfilenam e).readlines():
#execute all of the -e statements.
for command in optlist:
#warning bad mojo here
foo=(command[1] % line.rstrip("\n "))
exec(("line=%s" % foo))
#print line
#save to the new file
print line
newfile.write(l ine + "\n")
newfile.close()
os.unlink(oldfi lename)
############end bad code########### #######
The above code runs, but is not very good because I'm not that familiar
with exec statements. Anyway I've tried to capture what "perl -pi -e"
actually does which is to execute an arbitrary command over every line of an
arbitrary list of files, editing them in place, with a temporary backup
copy.
Then you would call it with something like:
python badscript.py -e 're.sub("foo"," bar","%s")' badtest.txt
However this is a place where an implicit loop works great.
You can just do:
perl -pi -e 's/foo/bar/' filelist
Or if you hate the perl/sed syntax, there is:
gawk '{gsub("foo", "bar", $0); print > FILENAME}' filelist
Both of these work because perl and awk have mechanisms to implicitly
loop over all the lines in a file. The python way tends to avoid
implicit loops except for a few cases.
Steven Rumbalski wrote:[color=blue]
> Olivier Scalbert wrote:
>
>[color=green]
>>Jarek Zgoda wrote:
>>
>>[color=darkred]
>>>Olivier Scalbert <olivier.scalbe rt@algosyn.com> pisze:
>>>
>>>
>>>>What is the python way of doing this :
>>>>perl -pi -e 's/string1/string2/' file
>>>>
>>>
>>>Use sed.
>>>[/color]
>>
>>yes, but in python ?[/color]
>
>
> print 'Use sed.'[/color]
Yes, but you're assuming that the users are using Unix/linux. What's
about the windows users? Perhaps there is a sed for windows already, but
why to bother installing it?
On Tue, 11 May 2004 11:16:01 +0200, Josef Meile <jmeile@hotmail .com>
wrote:
[color=blue]
>Steven Rumbalski wrote:[color=green]
>> Olivier Scalbert wrote:
>>
>>[color=darkred]
>>>Jarek Zgoda wrote:
>>>
>>>
>>>>Olivier Scalbert <olivier.scalbe rt@algosyn.com> pisze:
>>>>
>>>>
>>>>>What is the python way of doing this :
>>>>>perl -pi -e 's/string1/string2/' file
>>>>>
>>>>
>>>>Use sed.
>>>>
>>>
>>>yes, but in python ?[/color]
>>
>>
>> print 'Use sed.'[/color]
>Yes, but you're assuming that the users are using Unix/linux. What's
>about the windows users? Perhaps there is a sed for windows already, but
>why to bother installing it?[/color]
There's definitely a sed available, possibly even in MingW (I have it
on my system, but am not sure if it arrived with MingW or something
else I installed). It's definitely available with cygwin. One reason
to install it is that it's smaller than perl or python; another is
that it probably performs the task faster, since it isn't a general
purpose state machine; another is that it's 25% shorter to type than
perl and 50% shorter to type than python.
--dang
Kirk Job-Sluder <kirk@eyegor.jo bsluder.net> wrote in
news:slrnca0ub4 .1bdc.kirk@eyeg or.jobsluder.ne t:
[color=blue]
> The above code runs, but is not very good because I'm not that
> familiar with exec statements. Anyway I've tried to capture what
> "perl -pi -e" actually does which is to execute an arbitrary command
> over every line of an arbitrary list of files, editing them in place,
> with a temporary backup copy.[/color]
Your code might have been a bit shorter if you had used the existing
facility in Python for editing files in place. The code below is completely
untested, so I can all but guarantee it doesn't work, but you get the idea:
#!/usr/local/bin/python
import getopt,sys,os,r e
import fileinput
#get your command line options
#files will be in
optlist, args = getopt.getopt(s ys.argv[1:],'e:')
for line in fileinput.input (args, inplace=1):
#execute all of the -e statements.
for command in optlist:
#warning bad mojo here
foo=(command[1] % line.rstrip("\n "))
exec(("line=%s" % foo))
#save to the new file
print line
Comment