【问题标题】:Pass multiple arguments from c# to python将多个参数从 c# 传递给 python
【发布时间】:2018-10-29 00:53:05
【问题描述】:

我想将多个字符串变量从我的程序 C# 传递到 Python 脚本。

这是我在 C# 中的函数,我在其中调用文件 InsertSemantic.py,我想向那里传递 9 个参数(nome、formVerb、contesto ecc...)

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)而另一个参数没有通过...... 有人可以帮助我吗?

【问题讨论】:

  • 顺便说一句,Python 脚本可以更好nome, formVer, contesto, sinonimi, significato, gramm, prag, sem, retorico = sys.argv[1:10]

标签: c# python mysql wpf


【解决方案1】:
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 分配给时,只有第一个参数被传递,表明问题出现在任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2018-06-29
    • 2012-08-02
    • 2016-07-18
    相关资源
    最近更新 更多