如何使用C#在python中将字符串作为命令行参数传递

m2p*_*han 2 c# python processstartinfo command-line-arguments python-2.7

我尝试将字符串作为命令行参数发送到我的python脚本,如下所示:

var cmdToBeExecute = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\myScript.py";
            var start = new ProcessStartInfo
            {
                FileName = "C:\\Python27\\python.exe",
                Arguments = string.Format("{0} {1}", cmdToBeExecute, "Rahul done good"),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            };
Run Code Online (Sandbox Code Playgroud)

我试着读取命令行参数并在python中显示为:

import sys
print sys.argv[1]
Run Code Online (Sandbox Code Playgroud)

但是,我只得到第一个单词"Rahul"作为输出.请帮我把string作为命令参数参数发送到我的python脚本.

Sel*_*cuk 5

如果它包含空格,你应该在双引号内传递你的字符串:

Arguments = string.Format("{0} {1}", cmdToBeExecute, "\"Rahul done good\""),
Run Code Online (Sandbox Code Playgroud)

这不是C#特有的; 你应该使用引号,即使你从命令行运行它:

python myscript.py "Rahul done good"
Run Code Online (Sandbox Code Playgroud)