Nov-01-2020, 08:30 PM
I have zero experience with CGI, but the way you rebuild script looks very specific. I wrote something equally specific but use f'string.
It appears that all you are trying to do is get the key and value for the MiniFieldStorage, and it looks like there will always be two MiniFieldStorage objects in the list. Instead of trying to replace the parts that are formatted wrong I approached this by getting the parts I am interested in and building the string from fresh.
The end result is still a string, but hopefully a string in the right format for parsing.
It appears that all you are trying to do is get the key and value for the MiniFieldStorage, and it looks like there will always be two MiniFieldStorage objects in the list. Instead of trying to replace the parts that are formatted wrong I approached this by getting the parts I am interested in and building the string from fresh.
array_input = "FieldStorage None, None, [MiniFieldStorage 'v_name', 'John' , MiniFieldStorage 'v_sname', 'Aguiar' ]" fields = array_input.replace('FieldStorage None, None, [', '') # throw away stuff we don't care about fields = fields.replace('MiniFieldStorage', '').replace(']', '').replace(' ', '') fields = fields.split(',') # Get the things we want # Rebuild the string vbuild = f'FieldStorage(None, None, [MiniFieldStorage({fields[0]}, {fields[1]}), MiniFieldStorage({fields[2]},{fields[3]})]' print(vbuild)Im sure this could be made much better using regular expressions to find MiniFieldStorage and then getting the next two strings.
The end result is still a string, but hopefully a string in the right format for parsing.