[英]Execute Python Script With Parse Arguments From C#
as you can see in the pictures above i am trying to execute a python script which returns me recommended movies the inputs that python script is waiting for are the following:正如您在上面的图片中看到的,我正在尝试执行 python 脚本,该脚本返回给我推荐的电影 python 脚本正在等待的输入如下:
--movie_name "Iron Man" --top_n 10 1) accepted way from python --movie_name "钢铁侠" --top_n 10 1) 从 python 接受的方式
"--movie_name" "Iron Man" "--top_n" "10" 2) accepted way from python "--movie_name" "钢铁侠" "--top_n" "10" 2) 从 python 接受的方式
The last hours im searching for a way to give them properly but i cant.最后几个小时我正在寻找一种正确给予它们的方法,但我不能。 You can see in picture my last attempt.
你可以在图片中看到我的最后一次尝试。 Image from Python , Image from vsCode
图片来自 Python ,图片来自 vsCode
string s = "\"Iron Man\"";
string s1 = "\" --movie_name\"";
string s2 = "\" --top_n \"";
string s3 = "\"10\"";
string arg = string.Format(@"\c C:\Users\Azizmaiden\Desktop\files\hello\KnnRecommender.py {0} {1} {2} {3}", s1, s, s2, s3);
try
{
Process p1 = new Process();
p1.StartInfo.FileName = arg;
p1.StartInfo = new ProcessStartInfo(@"cmd.exe ", arg);
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.RedirectStandardInput = true;
p1.StartInfo.UseShellExecute = false;
p1.Start();
p1.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("There is a problem in your Python code: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
just try these first lines instead:只需尝试这些第一行:
string s = "Iron Man";
string s1 = "--movie_name";
string s2 = "--top_n";
string s3 = "10";
string arg = string.Format(@"/c ""C:\Users\Azizmaiden\Desktop\files\hello\KnnRecommender.py"" {0} ""{1}"" {2} {3}", s1, s, s2, s3);
All argument names itself like --movie_name have to be without "所有参数名称本身,如 --movie_name 必须没有 "
Your movie name has to be sorrounded by ", which is easier to read inside your format.您的电影名称必须用 " 包围,这样在您的格式中更容易阅读。
If your KnnRecommender.py file ever ends up in a path containing a space, you have to surround it with " anyways.如果您的 KnnRecommender.py 文件最终位于包含空格的路径中,则无论如何都必须用 " 包围它。
Found the solution, and is the following.找到了解决方案,如下。
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
string s1 = "--movie_name";
string s2 = "\"Iron Man\"";
string s3 = "--top_n";
string s4 = "10";
process.StartInfo = new ProcessStartInfo(@"cmd.exe ", @"/c C:\Users\Azizmaiden\Desktop\files\hello\KnnRecommender.py " + s1 + " " + s2 + " " + s3 + " " + s4)
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
Verb = "runas"
};
process.Start();
string res = "";
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd());
}
string r = q.ToString();
res = r;
Console.Write("RESULTS: " + res.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.