c# - 将多个参数从 c# 传递到 python

标签 c# python mysql wpf

我想将多个字符串变量从我的程序 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),而另一个参数没有通过...... 有人可以帮助我吗?

最佳答案

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 时,只有第一个参数被传递,这表明问题是在分配时。

关于c# - 将多个参数从 c# 传递到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413721/

相关文章:

python - 为什么这个 for 循环会乘以所有的东西?

python - 在for循环中使用python numpy linspace

php - 同时使用 gettext 和数据库驱动的翻译

php - Laravel Collection::toArray() 触发额外的数据库查询

c# - 为什么以相反的顺序调用构造函数?

python - 使用Inspect模块执行特定代码

C# WPF OxyPlot x, y 应该具有相同的缩放比例

mysql - 在 MySQL 中加入查询

c# - 如何使 wpf 组合框拉出特殊属性

c# - C# 中的“向后”公钥/私钥加密,我该怎么做?