C#调用带参数的python脚本

问题描述:使用C#调用下面的带参数的用python写的方法,并且想要获取返回值。

def Quadratic_Equations(a,b,c):
    D=b**2-4*a*c
    ans=[]
    ans.append((-b+math.sqrt(D))/(2*a))
    ans.append((-b-math.sqrt(D))/(2*a))
    return ans

C#代码如下:

class Program
    {
        static void Main(string[] args)
        {
            string name = "CallPythonExam.py";
            List<string> param = new List<string>() { "3", "5", "1" };
            RunPythonScript(name, param);
        }

        static void RunPythonScript(string name, List<string> args)
        {
            Process p = new Process();
            // .py文件的绝对路径
            string path = @"D:\PythonPrograms\VelocityProfile\VelocityProfile\" + name;
            string arguments = path;
            // 添加参数
            foreach (var item in args)
                arguments += " " + item;
            // python安装路径
            p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe";

            // 下面这些是我直接从网上抄下来的.......
            p.StartInfo.Arguments = arguments;
            // 不启用shell启动进程
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardError = true;
            // 不创建新窗口
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            StreamReader sr = p.StandardOutput;
            while (!sr.EndOfStream)
                Console.WriteLine(sr.ReadLine());
            Console.ReadLine();
            p.WaitForExit();
        }
    }

python代码如下:

import math
import sys

def Quadratic_Equations(stra,strb,strc):
    a=float(stra)
    b=float(strb)
    c=float(strc)
    D=b**2-4*a*c
    ans=[]
    ans.append((-b+math.sqrt(D))/(2*a))
    ans.append((-b-math.sqrt(D))/(2*a))
    print(ans[0])
    print(ans[1])
    #return str(ans[0])

Quadratic_Equations(sys.argv[1],sys.argv[2],sys.argv[3])

这样就可以在屏幕上输出方程的解。

https://github.com/Larissa1990/use-C-to-call-.py-file

原文地址:https://www.cnblogs.com/larissa-0464/p/11965564.html

时间: 2024-08-08 22:23:41

C#调用带参数的python脚本的相关文章

调用带参数的函数

函数声明语法:public static void 函数名(形参列表){ //注释内容} 函数 声明 时使用的参数,叫做形参 书写语法:数据类型 形参名→ 多个形参之间使用英文逗号分隔 代码示例: static void Test(int a , int b){ //函数体} → 调用 :Test(3 , "abc"); 实参不能标注类型 实参变量名与形参无关 在调用带参数的函数时无论实参是值.变量.表达式,仅需保证实参的数量和类型与形参匹配函数 声明 时使用的参数,叫做形参书写语法:

调用带参数的线程两种方法

第一种,用无参方法调用代参方法,用线程调用无参方法 第二种,如代码: //带参数的方法 ParameterizedThreadStart pt = new ParameterizedThreadStart(LoadGridView); //加入到线程 Thread thread = new Thread(pt); //允许后台执行 thread.IsBackground = true; //传入参数 thread.Start(""); 有参函数的参数要是object类型例如: priv

c# 多线程 调用带参数函数

线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函数(以下引自msdn).       Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.     Thread (ThreadStart) 初始化 Thread 类的新实例.    由 .NET Compact Framework 支持.        Threa

shell脚本调用带参数的存储过程

主要有 #!/bin/bash source /etc/profile source ~/.bash_profile ################################################################## #功能描述:调用带有输入参数和输出参数的存储过程 #执行周期:日 # CRONTAB: * 1 * * * * ${prog_path}CALL_PRODEDURE.sh >> ${prog_path}CALL_PRODEDURE.log 2&a

在Java中调用带参数的存储过程

JDBC调用存储过程: CallableStatement 在Java里面调用存储过程,写法那是相当的固定: Class.forName(.... Connection conn = DriverManager.getConnection(.... /** *p是要调用的存储过程的名字,存储过程的4个参数,用4个?号占位符代替 *其余地方写法固定 */ CallableStatement cstmt = conn.prepareCall("{call p(?,?,?,?)}"); /*

ado.net 调用带参数的存储过程

String connString = "Data Source = localhost; Initial Catalog = hkjc;User ID = sa;Pwd = 123";            SqlConnection conn = new SqlConnection(connString);            conn.Open();            SqlCommand cmd = new SqlCommand("GetManagement&q

线程调用带参数的方法

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading;using System.Threading.Tasks; namespace t{ /// <summary> /// get访问判断状态码 /// </summary> public class Filte

在C#中怎么调用带参数的存储过程啊??

1)执行一个没有参数的存储过程的代码如下:SqlConnection conn=new SqlConnection(“connectionString”);SqlDataAdapter da = new SqlDataAdapter();da.selectCommand = new SqlCommand();da.selectCommand.Connection = conn;da.selectCommand.CommandText = "NameOfProcedure";da.sel

20150825 C# 调用带参数的存储过程 模板

////// exec proceudre2                        //System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection("server=(local);database=pubs;uid=sa;pwd=;");                        System.Data.SqlClient.SqlConnection sqlcon