473,407 Members | 2,629 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,407 software developers and data experts.

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="something 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.iteritems():
h1=h1.replace(key, 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 1326
Harald Armin Massa wrote:
sth. like
rpdict={"<!--tree-->":"%(tree)s","<!--house-->":"%(house)s","%","%%"}

for key, value in rpdict.iteritems():
h1=h1.replace(key, 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="something 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
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...
4
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...
32
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...
4
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...
2
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...
60
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...
3
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
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...
4
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...
0
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 project—planning, coding, testing,...
0
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...

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.