473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re: python equivalent to perl's inplace edit mechanism

En Thu, 08 May 2008 09:11:56 -0300, Michael Mabin <d3******@gmail.comescribió:
Does python have an equivalent to Perl's inplace-edit variable $^I?
For example, the following perl code below changes mike to dave in a file
that is passed as an argument.

#!/usr/bin/env perl
#chgit script
$^I = '';
while(<>) {
s/mike/dave/g;
print;
}

The script would be used as below:
chgit somefile

Afterward, all instances of mike are changed to dave.
Like this?

<code>
import sys,fileinput

for line in fileinput.input(inplace=True):
sys.stdout.write(line.replace('mike','dave'))
</code>

Sample session:

C:\TEMP>type foo.txt
this line tells about mike and john
this second line says nothing
goodbye mike!

C:\TEMP>python chgit.py foo.txt

C:\TEMP>type foo.txt
this line tells about dave and john
this second line says nothing
goodbye dave!

--
Gabriel Genellina

Jun 27 '08 #1
0 1225

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
0
by: David | last post by:
#!/usr/bin/env perl #!/usr/bin/python import sys lines = open(sys.argv).readlines() open(sys.argv, 'w').writelines()
0
by: David | last post by:
On Thu, May 8, 2008 at 2:11 PM, Michael Mabin <d3vvnull@gmail.comwrote: I misread your question. No, Python eschews magic characters and symbols. They make code ugly and harder to read and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.