我只是一个代码的初学者,所以我不知道我是否在问一个愚蠢的问题,甚至可能会误用一些术语
我的问题是python中有一个字符串,其中包含一个可以理解为函数的文本,我想在列表中提取他的参数
字符串的示例:
line = """State("boatsmith search", "Search for the holder of the golden compass", delay=6)"""
作为处理该字符串的结果,我希望:
["boatsmith search", "Search for the holder of the golden compass", "delay=6"]
我不知道我说的是否清楚
我要找的东西是:
line = """State("boatsmith search", "Search for the holder of the golden compass", delay=6)"""
def function(x)
# magic with x
return list() # a list type result not actualy a list() xD
function(line)
>> ["boatsmith search", "Search for the holder of the golden compass", "delay=6)"]
事先非常感谢
您可以在下面尝试:
您可以使用
ast
模块解决此问题。它有ast.NodeVisitor
,这是一个基类,它遍历抽象语法树,并为找到的每个节点调用访问者函数您可以在
visit_Call
方法中处理不同的条件可以使用正则表达式:
输出:
在这种情况下,您可以使用:
输出:
相关问题 更多 >
编程相关推荐