Replace value from text in python
Question:
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
Answers:
This is where a regular expression might come in handy. Using re.sub
, you could do
re.sub(r'(?P<start>[.+?,s*)d+s+tos+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+tos+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+tos+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.
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
This is where a regular expression might come in handy. Using re.sub
, you could do
re.sub(r'(?P<start>[.+?,s*)d+s+tos+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+tos+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+tos+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.