1

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

  • https://stackoverflow.com/a/17141572/10557420 – Sabsa Aug 15 '22 at 10:41
  • @blackbrandt sorry it is in sdl format – user19640240 Aug 15 '22 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 ? – user19640240 Aug 15 '22 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? – Sabsa Aug 15 '22 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. – 9769953 Aug 15 '22 at 11:40
  • @Sabsa It is only the first two numbers the other number should still be the same. – user19640240 Aug 15 '22 at 13:32
  • I'm sorry, but with this little information it is hard to give an answer. Could you please read through my comment and the comment from @9769953 and answer our questions. – Sabsa Aug 15 '22 at 13:35
  • @9769953 Thanks for your input. I've updated my question. would you please have a look – user19640240 Aug 15 '22 at 13:55
  • To clarify: you have a string `1 to 3`, where both `1` and `3` could be any number? And you want to replace that string with another string like `4 to 11`, where both `4` and `11` are randomly generated numbers? – 9769953 Aug 15 '22 at 15:07
  • Also, the second ` to ` string, and the one with `Ego:-4 to -2` should be left alone and not changed, correct? – 9769953 Aug 15 '22 at 15:08
  • @9769953 That's completely correct. the 1 to 3 yes could be any number because every file has different number. and I use a random.uniform() to generate two random numbers , and what you said about the second x,y and ego also correct. – user19640240 Aug 15 '22 at 15:15

1 Answers1

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.

9769953
  • 10,344
  • 3
  • 26
  • 37