首页 > 编程语言 > 详细

使用.net执行python代码

时间:2020-07-16 13:30:39      阅读:80      评论:0      收藏:0      [点我收藏+]

以前听说过.net可以执行python代码,这次终于派上用场了
NUGET 安装 IronPython .NET版本4.8以下选择 2.7.8 这个版本

 public static string ExecPython(string pyCode)
       {
          ScriptEngine engine = Python.CreateEngine();
          ScriptScope scope = engine.CreateScope();
          var sourceCode = engine.CreateScriptSourceFromString(pyCode);
          var result = sourceCode.Execute<object>(scope);
          return result.ToString();
      }

关键词需要符合python的语法规范
技术分享图片

对于简单表达式,自带的DataTable也能解析,测试无法判断==
但是速度比py的快

string expression = "1+1";
DataTable eval = new DataTable();
object result = eval.Compute(expression, "");
Console.WriteLine(result);

使用.net执行python代码

原文:https://www.cnblogs.com/trykle/p/13321509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!