0

I've the following line in my file test.sdl:

PHASE 1: [Walk_Towards] [-, 1 to 3, -3 to -5][Ego:-4 to -2, FSL]

I want to replace the 1 to 3 with new random numbers (x,y) like 4 to 11. This two numbers are not fixed and generated by a random method and changing for every iteration.

Note the 1 to 3 also not fixed and it is varied for every file

all the files have the same structure Like

PHASE 1: [] [, to ,  to ][Ego: to , FSL]

So I would like to change only the numbers in between the first 'to'

My ultimate goal is to do an iteration lets say of 6. For every iterate the numbers will change

for example :

The current line is : PHASE 1: [Walk_Towards] [-, 1 to 3, -3 to -5][Ego:-4 to -2, FSL]

Second iteration will be based on a pre-defined variables (x,y) that changes every time randomly. So the result will be

`PHASE 1: [Walk_Towards] [-, x2 to y3, -3 to -5][Ego:-4 to -2, FSL]`

Third : PHASE 1: [Walk_Towards] [-, x2 to y3, -3 to -5][Ego:-4 to -2, FSL]

And so

This is the only numbers that i need to replace in the file. The format of the PHASE 1 is standard the only values change are numbers I used:

for line in lines:
        if 'PHASE 1:' in line:
            print(line)
            print(line.split('[')[2].split(','))

I could manage to get the following data: ['-', ' 1 to 3', ' -3 to -5]']

But I could not go beyond that How can take these values and replace them with my own and change the file for that. Any help would be appreciated

11
  • stackoverflow.com/a/17141572/10557420 Commented Aug 15, 2022 at 10:41
  • @blackbrandt sorry it is in sdl format Commented Aug 15, 2022 at 10:44
  • @Sabsa Thanks. I've read that but i did not know how to select the numbers to replace them. is it by index or what ? Commented Aug 15, 2022 at 10:45
  • Could you ellaborate your question? How do you know what numbers to change to? And do you know what you want to change from or do you only know the position of the string you want to replace? Commented Aug 15, 2022 at 11:40
  • Is it a fixed string you want to replace, or can the numbers change. For example, could it also be 2 to 4 -> 5 to 12, or 21 to 31 -> 55 to 66? And in that case, is there a logic in the replacement (for example, old numbers + x amount)? Because one is a simple string replacement (1 to 3 -> 4 to 11), the other means parsing the numbers and doing a calculation. A few more examples with an explanation of the possible logic would be useful. Commented Aug 15, 2022 at 11:40

1 Answer 1

2

This is where a regular expression might come in handy. Using re.sub, you could do

re.sub(r'(?P<start>\[.+?,\s*)\d+\s+to\s+\d+\s*,', f'\g<start>{n1} to {n2},', line)

with n1 and n2 the randomly generated substitution values.

To explain that a bit more, here's a fuller code sample, with the verbose flag turned on and comments to explain the regular expression a bit:

import re

line = "PHASE 1: [Walk_Towards] [-, 1 to 3, -3 to -5][Ego:-4 to -2, FSL]"
n1, n2 = 4, 11

print(re.sub(r'(?P<start>\[.+?,\s*)\d+\s+to\s+\d+\s*,', f'\g<start>{n1} to {n2},', line))

newline = re.sub(
    r'(?P<start>'     # Capture opening pattern
    '\[.+?,\s*'       # match: opening bracket + any characters up to a comma + the comma itself + any whitespace
    ')'               # Close capturing group
    '\d+\s+to\s+\d+'  # match digit(s) + whitespace + 'to' + whitespace + digit(s)
    '\s*,',           # match any optional whitespace + the comma

    f'\g<start>{n1} to {n2},',  # replacement: opening pattern + "n1 to n2," using an f-string
    line,
    flags=re.VERBOSE
)
print(newline)

which prints

PHASE 1: [Walk_Towards] [-, 4 to 11, -3 to -5][Ego:-4 to -2, FSL]

If the opening pattern is always [-, , it can probably be a bit simpler. But the above also allows for input like ...Towards] [7 to 11, 1 to 3, -3 to -5][... and it will still only replace the middle 1 to 3 only.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.