C是被解析的python代码树:
c=ast.parse(''' for x in y: print(x) ''')
D是树-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。
有什么建议吗?
谢谢
发布于 2018-07-16 17:52:59
可以使用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)。
https://stackoverflow.com/questions/51366393
复制相似问题