I have a file with strings like this:
Text_0_0
Text_0_1
...
Text_0_19
Text_1_0
...
Text_39_19
I want to replace it with values that has to be calculated out of the numeric values:
Text_x_y
→ Text_z
Where z = x * 20 + y
.
So after the operation the file should look like this:
Text_0
Text_1
...
Text_19
Text_20
...
Text_399
With the following code I can find the numeric values and calculate the correct number:
import re
import sys
f = open("file.txt", "r")
f.seek(0)
for line in f:
data = re.findall("Text_(\d*)_(\d*)", line)
if data is not None:
if len(data) == 1:
if len(data[0]) == 2:
number= int(data[0][0]) * 20 + (int(data[0][1]) + 1)
print (number)
f.close()
But how to write the correct values into the file?