c 是 python 代码的解析树:
c=ast.parse('''
对于 y 中的 x:
打印(x)
''')
d是tree-c的转储文件
d=ast.dump(c)
d的内容是以下字符串:
Module(
body=[For(target=Name(id='x', ctx=Store()), iter=Name(id='y', ctx=Load()),
body=[Expr(value=Call(func=Name(id='print', ctx=Load()),
args=[Name(id='x', ctx=Load())], keywords=[]))], orelse=[])])
我环顾网络,看看是否可以找到一种方法来使用 d 的内容,以恢复到树 c。
有什么建议吗?
谢谢
最佳答案
您可以使用eval
从字符串中获取ast
对象:
ast_obj = eval(d)
然后,存在各种可以转换回源代码的第三方库( Given an AST, is there a working library for getting the source? 、 Python ast (Abstract Syntax Trees): get back source string of subnode )
关于python - 有什么方法可以从树的转储文件中恢复 AST 树吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366393/