C# 调用 python3
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# 调用 python3的更多相关文章
- sublime COMMAND + B 调用 python3 运行
用sublime写了python3的代码,COMMAND + B运行调用 PYTHON3 我们先来新建一个sublime build system 然后自动打开了一个文本,清空并写入以下内容: { & ...
- notepad++调用python3中文乱码
使用notepad++,配置好快捷键调用python3,一切就绪,仿佛就差代码了,结果一使用, 中文乱码,一直没有好的解决办法. 最后只能在代码中增加一行重写向输出解决,示例如下: #!/usr/bi ...
- C++ 调用Python3
作为一种胶水语言,Python 能够很容易地调用 C . C++ 等语言,也能够通过其他语言调用 Python 的模块. Python 提供了 C++ 库,使得开发者能很方便地从 C++ 程序中调用 ...
- C++程序调用python3
今天想做一个简单的管理密码的小程序,由于最近了解了下Python,就想用Python来写.但是看了看Python的界面库用法有感觉有点麻烦,所以还不如直接使用MFC写写界面,关于csv的文件处理部分使 ...
- python2.7和python3共存
python2.7和python3共存 原本装了python,玩nodejs的时候需要node-gyp来编译依赖,无赖这货需要python2.5<v<3.0,那就弄两个版本吧 转载自 ht ...
- Linux下编译安装python3
Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装py ...
- Windows2012中Python2.7.11+Python3.4.4+Pycharm
下载软件包 Python2.7.11: https://www.python.org/ftp/python/2.7.11/python-2.7.11.amd64.msi Python3.4.4: ...
- 超详细的 Linux CentOS 编译安装python3
前言: 安装完CentOS7后,执行#Python与#python -V,看到版本号是2.6,而且之前写的都是跑在python3.X上面的,3.X和2.X有很多不同,在这里我就不弊述两者之间的区别了新 ...
- 如何在命令行中让python2和python3同存
初学python,你可能同时安装了python2和3.在我们安装好python之后,我们会面临这样一个问题,在命令行输入"python",可能会出错,或者只能调用其中一个版本,py ...
随机推荐
- URL锚点HTML定位技术机制、应用与问题
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=3591 一.锚点是什么 ...
- C语言获取文件大小相关操作
C语言获取文件大小相关操作 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明 通常在希望从文件中把数据全都出来赋值给一个数组或者某一个指针,然后再进行相关 ...
- 记录:larvel Windows 安装
先安装Composer 不多叙述了 链接: https://pan.baidu.com/s/1sljuImh 密码: qcj3 打开命令窗口 进入你的集成环境 网站根目录下 laravel程序会下载 ...
- 最新 乐游网络java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.乐游网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了乐游网络.6.7月主要是做系统复习.项目复盘.Leet ...
- word模板文档填充数据
1.切记一定要用Word 97-2003(*.doc)格式的文档另存为 *.xml格式的文件,而不能用 Word 2007(*.docx)格式的word文档转 *.xml,将转成功之后的xml文件放入 ...
- layer弹出层,结合art-template实现弹出编辑
模板 <!-- 模板 --> <script id="render-tpl" type="text/html"> <div cla ...
- C5. Spring 服务的注册与发现(Spring Cloud Eureka)
[概述] Eureka 作为 Spring Cloud 分布式解决方案中重要的一环,实现了服务的注册与发现等功能.Eureka 包括 Eureka Server 和 Eureka Client,具体的 ...
- Linux-BSP-驱动-面试题大全
1. 了解Linux的那个驱动?举例讲讲. a.驱动注册过程:通过platform bus, platform_device_register和platform_driver_register时都会在 ...
- uboot 与 代码重定位
ref: https://blog.csdn.net/dhauwd/article/details/78566668 https://blog.csdn.net/yueqian_scut/articl ...
- Linux 截取日志命令
1.截取时间段 截取 2019-06-25 10:10 到 2019-06-25 10:20 之间的日志记录,apollo-service.log 为你要截取的文件名称, new2.log 截取之后保 ...