spidee
spidee

Reputation: 547

How to replace digits in string?

Ok say I have a string in python:

str="martin added 1 new photo to the <a href=''>martins photos</a> album."

the string contains a lot more css/html in real world use

What is the fastest way to change the 1 ('1 new photo') to say '2 new photos'. of course later the '1' may say '12'. Note, I don't know what the number is, so doing a replace is not acceptable. I also need to change 'photo' to 'photos' but I can just do a .replace(...). Unless there is a neater, easier solution to modify both?

Upvotes: 1

Views: 2780

Answers (3)

Mark Tolonen
Mark Tolonen

Reputation: 178399

It sounds like this is what you want (although why is another question :^)

import re

def add_photos(s,n):
    def helper(m):
        num = int(m.group(1)) + n
        plural = '' if num == 1 else 's'
        return 'added %d new photo%s' % (num,plural)
    return re.sub(r'added (\d+) new photo(s?)',helper,s)

s = "martin added 0 new photos to the <a href=''>martins photos</a> album."
s = add_photos(s,1)
print s
s = add_photos(s,5)
print s
s = add_photos(s,7)
print s

Output

martin added 1 new photo to the <a href=''>martins photos</a> album.
martin added 6 new photos to the <a href=''>martins photos</a> album.
martin added 13 new photos to the <a href=''>martins photos</a> album.

Upvotes: 2

Manoj Govindan
Manoj Govindan

Reputation: 74795

Update

Never mind. From the comments it is evident that the OP's requirement is more complicated than it appears in the question. I don't think it can be solved by my answer.

Original Answer

You can convert the string to a template and store it. Use placeholders for the variables.

template = """%(user)s added %(count)s new %(l_object)s to the 
      <a href='%(url)s'>%(text)s</a> album."""

options = dict(user = "Martin", count = 1, l_object = 'photo', 
      url = url, text = "Martin's album")

print template % options

This expects the object of the sentence to be pluralized externally. If you want this logic (or more complex conditions) in your template(s) you should look at a templating engine such as Jinja or Cheetah.

Upvotes: 3

aaronasterling
aaronasterling

Reputation: 71064

since you're not parsing html, just use an regular expression

import re

exp = "{0} added ([0-9]*) new photo".format(name)
number = int(re.findall(exp, strng)[0])

This assumes that you will always pass it a string with the number in it. If not, you'll get an IndexError.

I would store the number and the format string though, in addition to the formatted string. when the number changes, remake the format string and replace your stored copy of it. This will be much mo'bettah' then trying to parse a string to get the count.

In response to your question about the html mattering, I don't think so. You are not trying to extract information that the html is encoding so you are not parsing html with regular expressions. This is just a string as far as that concern goes.

Upvotes: 2

Related Questions