zoukankan      html  css  js  c++  java
  • 使用C#调用Python脚本,带参数列表 z

    static void Main(string[] args)
            {
                string[] strArr;//参数列表
             string sArguments = @"Pythons.py";//这里是python的文件名字
             RunPythonScript(sArguments, "-u", strArr);
        
                }
    
    public static void RunPythonScript(string sArgName, string args = "",params string[] teps)
            {
                Process p = new Process();
                string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 获得python文件的绝对路径     
                p.StartInfo.FileName = @"python";
                string sArguments = path;
                if (tep.Length > 0)
                {
                    foreach (string sigstr in teps)
                    {
                        sArguments += " " + sigstr;//传递参数
                    }
                }
                if (args.Length > 0)
                {
    
                    sArguments += " " + args;
    
                }
    
                p.StartInfo.Arguments = sArguments;
    
                p.StartInfo.UseShellExecute = false;
    
                p.StartInfo.RedirectStandardOutput = true;
    
                p.StartInfo.RedirectStandardInput = true;
    
                p.StartInfo.RedirectStandardError = true;
    
                p.StartInfo.CreateNoWindow = true;
    
                p.Start();
                p.BeginOutputReadLine();
                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                Console.ReadLine();
                p.WaitForExit();
            }
            //输出打印的信息
            static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                if (!string.IsNullOrEmpty(e.Data))
                {
                     AppendText(e.Data + Environment.NewLine);
                }
            }
            public delegate void AppendTextCallback(string text);
            public static void AppendText(string text)
            {
                Console.WriteLine(text);
               
            }

    传递参数时,每个参数中间要有一个空格

    Python接收参数的方法:
    从1开始接收参数

    args1= sys.argv[1]

    args2=sys.argv[2]

    args2=sys.argv[3]

  • 相关阅读:
    发布spring cloud + vue项目
    以太坊上发行ERC20代币
    [转]大白话讲解Promise(一)
    比特币测试网络搭建
    非对称加密, 助记词, PIN, WIF
    搭建EOS未完
    [转]EOS智能合约 & 私链激活 & 基本操作
    [转]https://www.jianshu.com/p/06443248f4d8
    web3js 进行转账
    [转]How to Send Ethereum with Web3.js and Node
  • 原文地址:https://www.cnblogs.com/zeroone/p/3632371.html
Copyright © 2011-2022 走看看