Gonna turn this thread into a generic Python help thread. Hope @afre don’t mind.
Here’s a code I’m working on:
def change_in_between_brackets(str_inp):
current_str=[]
temp_arr=[]
ind=0
use_current=bool(True)
for char in str_inp:
if use_current:
if char!='{':
current_str.append(char)
else:
use_current=bool(False)
temp_arr.append(char)
ind += 1
else:
if char=='{':
ind += 1
temp_arr.append(char)
elif char=='}':
ind -= 1
if ind==0:
temp_arr.append(char)
new_string=""
for c in temp_arr:
new_string=new_string+c
new_string=new_string[1:-1]
new_string=new_string.replace('{','(')
new_string=new_string.replace('}',')')
new_string="{"+new_string+"}"
current_str.append(new_string)
temp_arr.clear()
else:
temp_arr.append(char)
else:
temp_arr.append(char)
print(''.join(map(str, current_str)))
str_inp="crop[$Index] {{{$Wtemp}-{$Htemp}}/2},0,{{$Htemp}+{{{$Wtemp}-{$Htemp}}/2}-1},{{$Htemp}-1}"
change_in_between_brackets(str_inp)
# It is missing some commas it seem. That's not right.
# crop[$Index] {(($Wtemp)-($Htemp))/2}{0,(($Htemp)+((($Wtemp)-($Htemp))/2)-1}{(($Htemp)-1}
I would like every {} inside the furthest-most {} to be (), and every commas to be kept.
Never mind, the solution was to add use_current=bool(True)
in the end of if ind==0:
.