从C#向Python传递多个参数

3

我希望能够从我的C#程序将多个字符串变量传递给Python脚本。

这是我的C#函数,我在其中调用InsertSemantic.py文件,并想将9个参数(名称,动词形式,上下文等)传递到该文件中。

private void insertDatabase(String nome, String formVerb, String contesto, String sinonimi, String significato, String class_gramm, String class_prag, String class_sem, String retorico)
{
    string arg = string.Format(@"C:\Users\marco\eclipse-workspace\my_project\src\InsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);
    try
    {
        Process p1 = new Process();
        p1.StartInfo = new ProcessStartInfo(@"C:\Python27\python.exe", arg);
        p1.StartInfo.UseShellExecute = false;
        p1.StartInfo.RedirectStandardOutput = true;
        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();
}

我已经在InsertSemantic.py中写入了这一行代码:
import sys

nome= sys.argv[1]
formVer=sys.argv[2]
contesto= sys.argv[3]
sinonimi=sys.argv[4]
significato=sys.argv[5]
gramm=sys.argv[6]
prag=sys.argv[7]
sem=sys.argv[8]
retorico=sys.argv[9]

但通过简单的打印,我发现我只收到了一个参数(nome),而另一个参数没有传递...... 有人可以帮我吗?


1
顺便提一下,Python脚本可以更加优美: nome, formVer, contesto, sinonimi, significato, gramm, prag, sem, retorico = sys.argv[1:10] - Sasha Tsukanov
3个回答

5
string arg = string.Format(@"C:\Users\marco\eclipse-workspace\my_project\src\InsertSemantic.py {0}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);

只有使用{0}时才能获取第一个格式值。您还需要包括其余的值,例如{0},{1},{2}等。

string arg = string.Format(@"C:\Users\marco\eclipse-workspace\my_project\src\InsertSemantic.py {0} {1} {2} {3} {4} {5} {6} {7} {8}", nome, formVerb, contesto, sinonimi, significato, class_gramm, class_prag, class_sem, retorico);

上述应包括添加到格式化字符串的其余参数。

在未来,您可以通过在调试器中逐步执行代码来解决此问题。您会注意到当赋值给arg时,只传递了第一个参数,表明问题出现在赋值时。


0
    public class CallPython
{
    public string Main(string[] args)
    {
        string arguments = args[0]; //Python script full file name including path always as the first argument
        for (int i = 1; i < args.Length; i++){
            arguments = arguments + " " + args[i]; //Add as many arguments as you need
            };
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python39_64\\python.exe";
        start.Arguments = arguments;
        start.UseShellExecute = false;// Do not use OS shell
        start.CreateNoWindow = true; // We don't need new window
        start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
        start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
        using (Process process = Process.Start(start)){
            using (StreamReader reader = process.StandardOutput){
                string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
                result = result.Replace("\n", "").Replace("\r", "");
                return result;}
                }
    }
}

测试 CallPython 主函数。 - moatasem chehaiber

0
 [Test]
        public void TestCreateArguments()
        {
            CallPython program = new CallPython();
            string cmd = "C:/IOIApp/Semantics.py";
            string[] inputs = {cmd, "test0", "test1", "test2" };
            string output_from_python=program.Main(inputs);
            string cleaned = output_from_python.Replace("\n", "").Replace("\r", ""); //you can do some cleaning here as well 
            Assert.AreEqual(cleaned, "test0");

        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接