Abijah
Abijah

Reputation: 546

Pythonically updating a value in a string

I have strings that may include numbers in square brackets. If they do I'd like to increment up the number in the final set of square brackets up by one.

old_content='Some text\t\t[1.00]\nSome other text'

So far i have code that does that, but doesn't feel pythonic.

open_indexes = [i for i, c in enumerate(old_content) if c == "["]
close_indexes = [i for i, c in enumerate(old_content) if c == "]"]
start       = old_content[:open_indexes[-1]+1]
end         = old_content[close_indexes[-1]:]
num         = old_content[open_indexes[-1]+1:close_indexes[-1]]
num         = float(num)+1
new_content =start +str(num) + end

Is there a more pythonic way of doing this?

Upvotes: 0

Views: 78

Answers (2)

Tomerikoo
Tomerikoo

Reputation: 19404

Using regex

Using the sub function of the re module, you can pass a function as the replacement:

import re

old_content = 'Some text\t\t[1.00]\nSome other text[3.00]'

def add_one_to_match(match):
    num = float(match[1])
    return f"[{num + 1}]"

new_content = re.sub(r'\[([^]]*)\][^[]*$', add_one_to_match, old_content)
print(repr(new_content))

Using string functions

You can double-partition the string from the right (to take only the last occurrence):

old_content = 'Some text\t\t[1.00]\nSome other text[3.00]'

start, _, end = old_content.rpartition('[')
num, _, end = end.rpartition(']')
print(repr(f"{start}[{float(num)+1}]{end}"))

Both ways will give:

'Some text\t\t[1.00]\nSome other text[4.0]'

Upvotes: 2

Timur Shtatland
Timur Shtatland

Reputation: 12347

Split the string into 3 parts: pre, number, post using re.findall. Increment the number by 1, and join the parts back into a single string.

import re
old_content = 'Some text\t\t[1.00]\nSome other text'
matches = list(re.findall(r"(.*\[)(.*)(\].*)", old_content, re.S)[0])
matches[1] = str(float(matches[1]) + 1)
new_content = "".join(matches)
print(new_content)
# Some text               [2.0]
# Some other text

Upvotes: 1

Related Questions