473,770 Members | 6,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple replaces

again, and again ... another try of templating

txt="""
<html>
<body>
<p>whatever</p>
<!--tree-->
<p>the machine with bing</p>
<!--house-->
<p>10% of boo is foo</p>
</html>"""

h1=txt.replace( "%","%%")
h2=h1.replace(" <!--tree-->","%(tree)s" )
h3=h2.replace(" <!--house-->","%(house)s ")

house="somethin g awfull"
tree="something beautifull"
print h3 % locals()
--> the <!--something--> approach allows it for me, that I can write
real HTML which looks fair. The replacing-chain takes care of % signs
within the file, and also makes a template for string replaces

BUT... it looks terribly inefficient to me. There are 3 strings which
are only there to be garbage collected. I am looking for a speedy way
of
"go through the text, and if you find sth. that is in REPLACEMENT,
replace it with it"

sth. like
rpdict={"<!--tree-->":"%(tree)s"," <!--house-->":"%(house)s", "%","%%"}

for key, value in rpdict.iteritem s():
h1=h1.replace(k ey, value)

but ... without the garbage, in one command.

I guess there are very, very, very wise solution for this problem, but
I do not know of them.

Who knows and tells me?

Harald

Sep 16 '05 #1
2 1336
Harald Armin Massa wrote:
sth. like
rpdict={"<!--tree-->":"%(tree)s"," <!--house-->":"%(house)s", "%","%%"}

for key, value in rpdict.iteritem s():
h1=h1.replace(k ey, value)

but ... without the garbage, in one command.

I guess there are very, very, very wise solution for this problem, but
I do not know of them.


http://effbot.org/zone/python-replace.htm

</F>

Sep 16 '05 #2
D H
You can use python's re.sub function. But also look into full fledged
template engines like Cheetah.

import re

txt="""
<html>
<body>
<p>whatever</p>
<!--tree-->
<p>the machine with bing</p>
<!--house-->
<p>10% of boo is foo</p>
</html>"""

h1=txt.replace( "%","%%")
h3 = re.sub("<!--(\w+)-->", "%(\\1)s", h1)

house="somethin g awfull"
tree="something beautifull"
print h3 % locals()

Sep 16 '05 #3

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

Similar topics

5
1269
by: David | last post by:
Hi, I have a replace function <%= Replace(arData(0,i), "_", " ")%> that works fine to replace the _ with a space, but I'd also like it to look for, and replace one word with another in the same string (E,G replace the word "Right" with "Wrong"). The code is located in a table that is displaying the results of a database search. I'm having problems with a) finding out if it's possible to do it this way, and b) what the syntax should...
4
3019
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email User1@here.whatever and User4@here.whatever but if "Loaded" is chosen it needs to email User2@here.whatever and User3@here.whatever, etc, etc... I'm aware that the only thing that really needs to change is the "Mail.AddAddress" line (at least...
32
14893
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
4
3131
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to replace multiple entries of an object within a string. That is, if a user hit twice to create one new line, but just once to create another, I want both types ( & twice to create a new line (by creating a blank line between paragraphs). ...
2
1777
by: Alan Foxmore | last post by:
Hi Everyone, I'm new to ASP.NET so maybe this is easy. I'm using ASP.NET 2.0. I'm finding that the Application object (the HttpApplicationState object) is allowing me to add the same key multiple times. The easiest way to describe the problem is to just show you. In my Page_Load() I have this code:
60
4939
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
3
1496
by: teo | last post by:
I have a text. Inside the text the "hallo" word occurs five time. I need to replace "hallo" with "hallo world". Unfortunately I get this: hallo world world world world world
5
1328
MitchR
by: MitchR | last post by:
Hello to All; I have inherited a database and need to have the database post a date in a comments column which I can do. Problem is every time I execute the event procedure it replaces the old date with the new date. I am trying to figure out how to keep the old date and place the new date as well. Here is my code below: Dim myDate As Variant myDate = Date !! = "Date of Notification From Mitch " & myDate & "; " The above code places my...
4
5054
by: Dan | last post by:
Hi all, I am creating a search table where the keywords field is made up of several text fields and this is causing me some problems. I can concatentate the text ok but i can't seem to concatenate matching records here is the cursor loop. I'm not a fan of cursors but also didn't see another way of achieving this. declare @ptr1 varbinary(16) declare @Ptr2 varbinary(16) declare @profileid int declare @x int
0
9425
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
10230
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
10058
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
10004
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,...
1
7416
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
5313
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...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.