小标
2018-11-14
来源 :
阅读 2847
评论 0
摘要:本文主要向大家介绍了C#编程之C# 调用 python3,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C# 调用 python3,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
1.C# 调用python
本质上是使用命令行运行python
1.1 C# 使用命令行
program.cs
using System;
using System.Diagnostics;
using System.IO;
namespace test
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
string result = p.run_cmd("ping.exe", "8.8.8.8 -n 2");
Console.WriteLine(result);
Console.ReadKey();
}
public string run_cmd(string program, string cmd)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = program;
start.Arguments = cmd;
start.UseShellExecute = false; // Do not use OS shell
start.CreateNoWindow = true; // We don‘t need new window
start.RedirectStandardOutput = true; // Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = process.StandardError.ReadToEnd();
if (result == null || result == "")
{
result = reader.ReadToEnd();
}
return result;
}
}
}
}
}
代码运行结果
调用run_cmd相当于执行了cmd命令,所以就有了使用命令行运行python脚本的方式
1.2. C# 调用 python3脚本
假设C盘根目录下有如下脚本 test1.py
import sys
def add(a,b):
return a+b
if __name__ == "__main__":
print(sys.argv[1])
print("hello python")
在 program.cs 中加入函数runPython,并修改main函数
static void Main(string[] args)
{
Program p = new Program();
//string result = p.run_cmd("ping.exe", "8.8.8.8 -n 2");
string result = p.runPython("C:\\test1.py", "\"Form C#:\"");
Console.WriteLine(result);
Console.ReadKey();
}
public string runPython(string filename, string cmd)
{
string cmd1 = string.Format("{0} {1}", filename, cmd);
return run_cmd("python.exe", cmd1);
}
代码运行结果
1.3 C# 调用python3内的函数
我们知道使用python -c可以直接执行python代码,所以合理构造语句就可以直接调用python脚本内的函数了:python -c "print(‘hello python‘)"。
若要调用脚本里的函数,常规写法为:
import sys
sys.path.append(‘c:\\‘)
import test1
print(test1.add(3,4))
缩成一行就是python -c "import sys;sys.path.append(‘c:\\‘);import test1;print(test1.add(3,4))"
在 program.cs 中加入函数runPyFunc,并修改main函数
static void Main(string[] args)
{
Program p = new Program();
//string result = p.run_cmd("ping.exe", "8.8.8.8 -n 2");
//string result = p.runPython("C:\\test.py", "\"Form C#:\"");
string result = p.runPyFunc(@"C:\\","test1","add","3,4");
Console.WriteLine(result);
Console.ReadKey();
}
public string runPyFunc(string path, string filename, string functionname, string parameter)
{
string cmd = string.Format("-c \"import sys;sys.path.append(‘{0}‘);import {1};print({1}.{2}({3}))\"", path, filename, functionname, parameter);
return run_cmd("python.exe", cmd);
}
运行就可以得到结果“7”了
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
喜欢 | 2
不喜欢 | 2
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号