Fork me on GitHub

python之如何执行用字符串表示的代码

比如说现在有这么一段字符串,"print('helloworld')",那么怎么将其转换成python认识的格式运行?

s="print('helloworld')"
r = compile(s,"<string>", "exec")
exec(r)

输出:helloworld

posted @ 2020-04-27 15:25  西西嘛呦  阅读(1764)  评论(0)    收藏  举报