473,908 Members | 6,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

perl to python

Hello ,

What is the python way of doing this :
perl -pi -e 's/string1/string2/' file
?
Thanks
Olivier
Jul 18 '05 #1
52 3795
Olivier Scalbert <ol************ **@algosyn.com> pisze:
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file


Use sed.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #2
Jarek Zgoda wrote:
Olivier Scalbert <ol************ **@algosyn.com> pisze:
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file


Use sed.

yes, but in python ?
Jul 18 '05 #3
Olivier Scalbert <ol************ **@algosyn.com> pisze:
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file


Use sed.


yes, but in python ?


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.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #4
Olivier Scalbert wrote:
Jarek Zgoda wrote:
Olivier Scalbert <ol************ **@algosyn.com> pisze:
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file

Use sed.

yes, but in python ?

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?

Pad.

Jul 18 '05 #5

"Olivier Scalbert" <ol************ **@algosyn.com> wrote in message
news:40******** *************** @news.skynet.be ...
Hello ,

What is the python way of doing this :
perl -pi -e 's/string1/string2/' file
?
Thanks
Olivier


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
Jul 18 '05 #6
John Roth wrote:
"Olivier Scalbert" <ol************ **@algosyn.com> wrote in message
news:40******* *************** *@news.skynet.b e...

Hello ,

What is the python way of doing this :
perl -pi -e 's/string1/string2/' file
?
Thanks
Olivier


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

Thx John !
Jul 18 '05 #7
John Roth wrote:
"Olivier Scalbert" <ol************ **@algosyn.com> wrote in message
news:40******** *************** @news.skynet.be ...
Hello ,

What is the python way of doing this :
perl -pi -e 's/string1/string2/' file
?
Thanks
Olivier

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


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+"))'

Jul 18 '05 #8
Olivier Scalbert <ol************ **@algosyn.com> writes:
Jarek Zgoda wrote:
Olivier Scalbert <ol************ **@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('stri ng1', '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
Jul 18 '05 #9
Olivier Scalbert wrote:
Jarek Zgoda wrote:
Olivier Scalbert <ol************ **@algosyn.com> pisze:
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file

Use sed.

yes, but in python ?


print 'Use sed.'
--
Steven Rumbalski
news|at|rumbals ki|dot|com
Jul 18 '05 #10

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

Similar topics

9
2151
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will be to convince them otherwise. The basic design of the application is object oriented. I've never used Perl's OO features, so I'm not in a good position to make a comparison of the two languages from an OO point of view. Can somebody who's...
42
4149
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the need to resort to something more expedient than C++, bash scripting, or sed scripting.
46
3332
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar step (-> Python 3) some time in the near future? Reinhold
17
3127
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
31
4830
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
9
4535
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions. I know that the web offers a lot of resources on Python/Perl differences. But I couldn't find a...
20
4103
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
13
2111
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
12
2373
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
8
2448
by: Palindrom | last post by:
Hi everyone ! I'd like to apologize in advance for my bad english, it's not my mother tongue... My girlfriend (who is a newbie in Python, but knows Perl quite well) asked me this morning why the following code snippets didn't give the same result : ### Python ###
0
9875
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11337
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10913
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7246
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4770
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.