DSF
DSF

Reputation: 21

How can I replace a number in a string with another number?

I am trying to replace a number in a string with another number. For instance, I have the string "APU12_24F" and I want to add 7 to the second number to make it "APU12_31F".

Right now I am simply able to locate the number in which I'm interested by using string.split.

I can't figure out how to edit the new strings which this produces.

def main():
    f=open("edita15888_debug.txt", "r")
    fl = f.readlines()
    for x in fl:
    if ("APU12" in x):
        list_string=split_string(x)
        print(list_string);
    return      

def split_string_APU12(string):
    # Split the string based on APU12_ 
    list_string = string.split("APU12_") 
    return list_string 

main()

The output for this makes sense as I'll get something like ['', 24F\n]. I just now need to change the 24 to 31 then put it back into the original string.

Feel free to let me know if there is a better approach to this. I'm very new to python and everything I can find online with the available search/replace functions doesn't seem to do what I'd need them to do. Thank you!

Upvotes: 2

Views: 1388

Answers (4)

Andrey Kachow
Andrey Kachow

Reputation: 1126

I assumed you need to add seven to a number which goes after an underscore. I hope, this function will be helpful

import re

def add_seven_to_number_after_underscore_in_a_string(aString):
    regex = re.compile(r'_(\d+)')
    match = regex.search(aString)
    return regex.sub('_' + str(int(match.group(1)) + 7), aString)

Upvotes: 0

Jacek Rojek
Jacek Rojek

Reputation: 1122

Assuming that pattern is _ + multiple digits you can replace it with regex

import re

re.sub(r"_(\d+)", lambda r: '_'+str(int(r.group(1)) + 7),'APU12_24F')

Upvotes: 4

RMoreno Itecnis
RMoreno Itecnis

Reputation: 33

I'm assuming your strings will be of the format

APU12_##...F

(where ###... means a variable digits number, and F could be any letter, but just one). If so, you could do something like this:

# Notice the use of context managers
# I would recommend learning about this for working with files
with open('edita15888_debug.txt', 'r') as f:
    fl = f.readlines()

new_strings = []
for line in fl:
    beg, end = line.split('_')

    # This splits the end part into number + character
    number, char = int(end[:-1]), end[-1]

    # Here goes your operation on the number
    number += your_quantity   # This may be your +7, for example

    # Now joining back everything together
    new_strings.append(beg + '_' + str(number) + char)

And this would yield you the same list of strings but with the numbers before the last letter modified as you need.

I hope this helps you!

Upvotes: 0

Sadrach Pierre
Sadrach Pierre

Reputation: 121

This isn't generalized because I'm not sure what the rest of the data looks like but maybe something like this should work:

def main():
    f=open("edita15888_debug.txt", "r")
    fl = f.readlines()
    for x in fl:
        if ("APU12" in x):
            list_string=split_string_APU12(x)
            list_string = int(list_string[1].split('F')[0]) + 7
            list_string = "APU12_" + str(list_string) 
            print(list_string)
    return      

def split_string_APU12(string):
    # Split the string based on APU12_ 
    list_string = string.split("APU12_") 
    return list_string 

main()

Upvotes: 0

Related Questions