Skip to content Skip to sidebar Skip to footer

Parse Statement String For Arguments Using Regex In Python

I have user input statements which I would like to parse for arguments. If possible using regex. I have read much about functools.partial on Stackoverflow where I could not find ar

Solution 1:

getarguments("psutil.disk_usage('/').percent") returns '/'. You can check this by printing len(arg_list), for example.

Your IDE adds ", because by default strings are enclosed into single quotes '. Now you have a string which actually contains ', so IDE uses double quotes to enclose the string.

Note, that '/' is not equal to "'/'". The former is a string of 1 character, the latter is a string of 3 characters. So in order to get things right you need to strip quotes (both double and single ones) in getarguments. You can do it with following snippet

if (s.startswith('\'') and s.endswith('\'')) or 
        (s.startswith('\"') and s.endswith('\"')):
   s = s[1:-1]

Post a Comment for "Parse Statement String For Arguments Using Regex In Python"