Hello Mat

 找回密码
 立即注册
查看: 7272|回复: 2

C#调用python脚本

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2020-5-23 18:38:56 | 显示全部楼层 |阅读模式
C#调用python脚本
  1. private Process progressTest;
  2.         public Form1()
  3.         {
  4.             InitializeComponent();
  5.         }

  6.         private void button1_Click(object sender, EventArgs e)
  7.         {
  8.             try
  9.             {
  10.                 string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
  11.                 int a = Convert.ToInt32(this.comboBox1.Text);
  12.                 int b = Convert.ToInt32(this.comboBox2.Text);
  13.                 bool trueorfalse = StartTest(path, a, b);

  14.                 //string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
  15.                 //string a = Convert.ToString(this.comboBox1.Text);
  16.                 //string b = Convert.ToString(this.comboBox2.Text);
  17.                 //Process p = new Process();
  18.                 ////string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
  19.                 //string sArguments = path;
  20.                 //ArrayList arrayList = new ArrayList();
  21.                 //arrayList.Add(a);
  22.                 //arrayList.Add(b);
  23.                 //foreach (string param in arrayList)//添加参数
  24.                 //{
  25.                 //    sArguments += " " + param;
  26.                 //}
  27.                 ////sArguments += " " + "-u";

  28.                 //p.StartInfo.FileName = @"D:\ProgramData\Anaconda3\python.exe";    //环境路径需要配置好
  29.                 //p.StartInfo.Arguments = sArguments;    // python命令的参数
  30.                 //p.StartInfo.UseShellExecute = false;
  31.                 //p.StartInfo.RedirectStandardOutput = true;
  32.                 //p.StartInfo.RedirectStandardInput = true;
  33.                 //p.StartInfo.RedirectStandardError = true;
  34.                 //p.StartInfo.CreateNoWindow = true;
  35.                 //p.EnableRaisingEvents = true;
  36.                 //p.Start();//启动进程

  37.                 ////Console.WriteLine("执行完毕!");
  38.                 ////p.BeginOutputReadLine();
  39.                 ////p.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
  40.                 ////Console.ReadKey();

  41.                 //p.WaitForExit();
  42.             }
  43.             catch (Exception e1)
  44.             {
  45.                 MessageBox.Show(e1.Message);
  46.             }
  47.         }
  48.         public bool StartTest(string pathAlg, int a, int b)
  49.         {
  50.             bool state = true;

  51.             if (!File.Exists(pathAlg))
  52.             {
  53.                 throw new Exception("The file was not found.");
  54.                 return false;
  55.             }
  56.             string sArguments = pathAlg;
  57.             sArguments += " " + a.ToString() + " " + b.ToString();//Python文件的路径用“/”划分比较常见

  58.             ProcessStartInfo start = new ProcessStartInfo();
  59.             start.FileName = @"D:\ProgramData\Anaconda3\python.exe";    //环境路径需要配置好
  60.             start.Arguments = sArguments;
  61.             start.UseShellExecute = false;
  62.             start.RedirectStandardOutput = true;
  63.             start.RedirectStandardInput = true;
  64.             start.RedirectStandardError = true;
  65.             start.CreateNoWindow = true;
  66.             using (progressTest = Process.Start(start))
  67.             {
  68.                 // 异步获取命令行内容
  69.                 progressTest.BeginOutputReadLine();
  70.                 // 为异步获取订阅事件
  71.                 progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
  72.             }
  73.             return state;
  74.         }
  75.         public void outputDataReceived(object sender, DataReceivedEventArgs e)
  76.         {
  77.             if (!string.IsNullOrEmpty(e.Data))
  78.             {
  79.                 this.Invoke(new Action(() =>
  80.                 {
  81.                     this.textBox1.Text = e.Data;
  82.                 }));
  83.             }
  84.         }
复制代码



参考:
【1】c#调用python的方法总结
【2】c#调用python脚本
【3】c#调用python的四种方法
【4】C#调用Python的使用总结

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2020-5-23 21:16:57 | 显示全部楼层
  1.                 // 方法2
  2.                 string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
  3.                 string a = Convert.ToString(this.comboBox1.Text);
  4.                 string b = Convert.ToString(this.comboBox2.Text);
  5.                 Process p = new Process();
  6.                 //string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
  7.                 string sArguments = path;
  8.                 ArrayList arrayList = new ArrayList();
  9.                 arrayList.Add(a);
  10.                 arrayList.Add(b);
  11.                 foreach (string param in arrayList)//添加参数
  12.                 {
  13.                     sArguments += " " + param;
  14.                 }
  15.                 //sArguments += " " + "-u";

  16.                 p.StartInfo.FileName = @"D:\ProgramData\Anaconda3\python.exe";    //环境路径需要配置好
  17.                 p.StartInfo.Arguments = sArguments;    // python命令的参数
  18.                 p.StartInfo.UseShellExecute = false;
  19.                 p.StartInfo.RedirectStandardOutput = true;
  20.                 p.StartInfo.RedirectStandardInput = true;
  21.                 p.StartInfo.RedirectStandardError = true;
  22.                 p.StartInfo.CreateNoWindow = true;
  23.                 p.EnableRaisingEvents = true;
  24.                 p.Start();    //启动进程
  25.                 p.WaitForExit();
复制代码
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2020-5-23 21:33:46 | 显示全部楼层
  1. import sys
  2. sys.path.append(r"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode")
  3. #import os
  4. #import argparse
  5. import numpy

  6. def add(a, b):
  7.     return a + b
  8. def mult(a, b):
  9.     return a * b

  10. if __name__ == "__main__":
  11.     a = float(sys.argv[1])
  12.     b = float(sys.argv[2])
  13.     txt_file = 'result.txt'
  14.     with open(txt_file,'a') as f:
  15.         f.write(str(a)+'\n')
  16.     f.close();
  17.     with open(txt_file,'a') as f:
  18.         f.write(str(b)+'\n')
  19.     f.close();
  20.    
  21.     c1 = add(a, b)
  22.     c2 = mult(a, b)
  23.     print(c1, c2)
  24.    
  25.     txt_file = 'result.txt'
  26.     with open(txt_file,'a') as f:
  27.         f.write(str(c1)+'\n')
  28.     f.close();
复制代码
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-25 07:24 , Processed in 0.227907 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表