best way to parse a function-call-like string?

Tim Wintle tim.wintle at teamrubber.com
Thu Feb 26 16:39:09 EST 2009


On Thu, 2009-02-26 at 21:29 +0000, mh at pixar.com wrote:
> I have some strings that look like function calls, e.g.
> 
> "junkpkg.f1"
> "junkpkg.f1()"
> "junkpkg.f1('aaa')"
> "junkpkg.f1('aaa','bbb')"
> "junkpkg.f1('aaa','bbb','ccc')"
> "junkpkg.f1('aaa','with,comma')"
> 
> and I need to split them into the function name and list of parms,
> e.g.
> 
> "junkpkg.f1", []
> "junkpkg.f1", []
> "junkpkg.f1", ['aaa']
> "junkpkg.f1", ['aaa','bbb']
> "junkpkg.f1", ['aaa','bbb','ccc']
> "junkpkg.f1", ['aaa','with,comma']
> 

quick and dirty

for s in string_list:
    if "(" in s and s[-1] == ")":
      parts = s.split("(")
      fn, args = s[0],s[1][:-1].split(",")


Tim Wintle




More information about the Python-list mailing list