John
John

Reputation: 356

python replace() not working as expected

My script is supposed to write html files changing the html menu to show the current page as class="current_page_item" so that it will be highlighted when rendered. It has to do two replacements, first set the previous current page to be not current, then set the new current page to current. The two writeText.replace lines do not appear to have any effect. It doesn't give me an error or anything. Any suggestions would be appreciated.

for each in startList:
    sectionName = s[each:s.find("\n",each)].split()[1]
    if sectionName[-3:] <> "-->":
        end = s.find("end "+sectionName+'-->')
        sectionText =  s[each+len(sectionName)+12:end-1]
        writeText = templatetop+"\n"+sectionText+"\n"+templatebottom
        writeText.replace('<li class="current_page_item">','<li>')
        writeText.replace('<li><a href="'+sectionName+'.html','<li class="current_page_item"><a href="'+sectionName+'.html')
        f = open(sectionName+".html", 'w+')
        f.write(writeText)
        f.close()  

Here is part of the string I am targeting (templatetop):

<li class="current_page_item"><a href="index.html" accesskey="1" title="">Home</a></li>
<li><a href="history.html" accesskey="2" title="">History</a></li>
<li><a href="members.html" accesskey="3" title="">Members</a></li>

Upvotes: 0

Views: 2150

Answers (3)

Lennart Regebro
Lennart Regebro

Reputation: 172209

So first you replace '<li class="current_page_item">' with '<li>' and then you replace '<li>' with '<li class="current_page_item">'. That's a bit funny, I have to say.

In addition to the problem pointed out by misha, that replace returns the result, your two replacements in fact cancel each other out.

>>> writeText = """<li class="current_page_item"><a href="index.html" accesskey="1" title="">Home</a></li>
... <li><a href="history.html" accesskey="2" title="">History</a></li>
... <li><a href="members.html" accesskey="3" title="">Members</a></li>"""
>>> result = writeText.replace('<li class="current_page_item">','<li>')>>> result = result.replace('<li><a href="index.html','<li class="current_page_item"><a href="index.html')
>>> result == writeText
True

Now this is just the first iteration of replacements, but it's a good indication that you are using the wrong solution. It also means you can simply remove the first of the replacements and it will still work.

Also, why are you doing the replacement on writeText, when you are only targeting templatetop?

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251365

You should not expect that to work, because you should read the documentation:

Return a copy of the string with all occurrences of substring old replaced by new.

Upvotes: 3

mpenkov
mpenkov

Reputation: 21896

replace returns the resulting string, so you need to do this:

writeText = writeText.replace('<li class="current_page_item">','<li>')
writeText = writeText.replace('<li><a href="'+sectionName+'.html','<li class="current_page_item"><a href="'+sectionName+'.html')

Upvotes: 6

Related Questions