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 ...
随机推荐
- FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated;
/Users/jerryqi/PycharmProjects/DeepLearning/venv/lib/python3.7/site-packages/tensorflow/python/frame ...
- Hbuider APP打包流程
1,下载HBuilder,注册并登陆.首先打开“文件”-“新建”-“移动APP”,输入“应用名称”,“位置”可以根据需要自己选择即可,“选择模板”建议选择空模板: 2,新建完成后, 在项目管理器会 ...
- Docker部署Gitlab11.10.4
1.下载镜像 官方镜像地址:https://hub.docker.com/r/gitlab/gitlab-ce ,根据自己需要下载指定版本 [root@vanje-dev01 ~]# docker p ...
- Vue中常用知识点demo
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Python 第一式
@Codewars Python练习 question ** Dashatize it ** Given a number, return a string with dash'-'marks bef ...
- java中的自动装箱和拆箱
一.什么是自动装箱和拆箱: 我们知道java为8种基本类型分别提供了对应的包装类型,在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i=new I ...
- (二十五)JSP九大内置对象(转)
--转载自孤傲苍狼博客 一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质 ...
- (七)发送、接收SOAP消息(以HttpClient方式)(2)
一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...
- 从jvm源码看synchronized
从jvm源码看synchronized 索引 synchronized的使用 修饰实例方法 修饰静态方法 修饰代码块 总结 Synchronzied的底层原理 对象头和内置锁(ObjectMonito ...
- RESTful接口开发
package com.aaaaaa.manager.controller; import org.springframework.beans.factory.annotation.Autowired ...