c调用python脚本如何获取结果_从C调用python脚本#

我有一个C#代码,它首先帮助运行python环境,然后执行python进程。但问题是它需要很多时间来执行。

实际上,我只想在python脚本中传递我的值并执行单行代码。但每次都需要执行所有的python代码。有没有一种方法可以在外部运行python进程,在我需要的时候只运行一行。

我用这个附加了C代码和python进程

C#代码public String Insert(float[] values)

{

// full path of python interpreter

string python = @"C:\ProgramData\Anaconda2\python.exe";

// python app to call

string myPythonApp = @"C:\classification.py";

// dummy parameters to send Python script

//int x = 2;

//int y = 5;

// Create new process start info

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

// make sure we can read the output from stdout

myProcessStartInfo.UseShellExecute = false;

myProcessStartInfo.RedirectStandardOutput = true;

myProcessStartInfo.CreateNoWindow = true;

myProcessStartInfo.WindowStyle = ProcessWindowStyle.Minimized;

// start python app with 3 arguments

// 1st arguments is pointer to itself, 2nd and 3rd are actual arguments we want to send

myProcessStartInfo.Arguments = myPythonApp + " " + values[0] + " " + values[1] + " " + values[2] + " " + values[3] + " " + values[4] + " " + values[5];

Process myProcess = new Process();

// assign start information to the process

myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();

// Read the standard output of the app we called.

// in order to avoid deadlock we will read output first and then wait for process terminate:

StreamReader myStreamReader = myProcess.StandardOutput;

string myString = myStreamReader.ReadLine();

/*if you need to read multiple lines, you might use:

string myString = myStreamReader.ReadToEnd() */

// wait exit signal from the app we called and then close it.

myProcess.WaitForExit();

myProcess.Close();

// write the output we got from python app

Console.WriteLine("Value received from script: " + myString);

Console.WriteLine("Value received from script: " + myString);

和python脚本import numpy as np

import sys

val1 = float(sys.argv[1])

val2 = float(sys.argv[2])

val3 = float(sys.argv[3])

val4 = float(sys.argv[4])

val5 = float(sys.argv[5])

val6 = float(sys.argv[6])

# Load dataset

url = "F:\FINAL YEAR PROJECT\Amila\data2.csv"

names = ['JawLower', 'BrowLower', 'BrowRaiser', 'LipCornerDepressor', 'LipRaiser','LipStretcher','Emotion_Id']

dataset = pandas.read_csv(url, names=names)

# shape

# print(dataset.shape)

# class distribution

# print(dataset.groupby('Emotion_Id').size())

# Split-out validation dataset

array = dataset.values

X = array[:,0:6]

Y = array[:,6]

neigh = KNeighborsClassifier(n_neighbors=3)

neigh.fit(X, Y)

print(neigh.predict([[val1,val2,val3,val4,val5,val6]]))

print(neigh.predict([[val1,val2,val3,val4,val5,val6]])这是我要分别执行的代码行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值