如何从C#中的python脚本获取返回值 [英] How to get the return value from python script in C#

查看:1694
本文介绍了如何从C#中的python脚本获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的所有人:

我正在使用.NET3.5 C#运行python脚本,并尝试从python获取返回值.
我将python脚本放在磁盘C://下.

然后在C#中使用以下命令运行python脚本:

Dear ALL:

I am using .NET3.5 C# run a python script and try to get the return value from python

I put the python script under disk C://.

Then in C# run the python script with:

ProcessStartInfo Info = new ProcessStartInfo();

                        Info.FileName = "getvalue.py";

                        Info.WorkingDirectory = @"c:";

                        Process Star = Process.Start(Info);



那我怎么能从python脚本中获取值.

例如,python脚本返回值"3".

任何提示都值得欢迎和赞赏.


问候

Kay



Then how could I get the value from python script.

For example python script return a value "3".

Any tips welcomed and appreciated.


Regards

Kay

推荐答案

有两种方法.

第一个非常有限,并且基于退出代码.在Windows和类似Unix的系统上,所有应用程序均返回默认为0的整数代码.传统上,0表示一切都OK",但也可以是任何东西.使用Python时,通过将整数值分配给特殊变量
There are two ways of doing it.

First one is very limited and based on exit code. On Windows and Unix-like systems, all applications return integer code which defaults to 0. Traditionally, 0 means "everything is OK", but it could be anything. With Python, exit code is returned by assigning an integer value to the special variable


?来返回退出代码.请参阅:
http://stackoverflow.com/questions/285289/exit-codes-in-python [ ^ ].

在.NET父进程的一侧,当子进程终止时,可以使用属性System.Diagnostics.Process.ExitCode:
获取退出代码. http://msdn.microsoft.com/en-us/library/system. diagnostics.process.exitcode.aspx [ ^ ].

您可以使用方法System.Diagnostics.Process.WaitForExit:
将父进程的线程与子进程的终止同步. http://msdn.microsoft.com/en-us/library/system.diagnostics. process.aspx [^ ].

请注意,这些方法是阻塞的,因此它们不适合从UI线程进行调用-您可能需要在单独的线程中使用System.Diagnostics.Process执行所有代码,至少在Python脚本需要执行以下操作的情况下需要相当多的时间来完成.

第二种方法比较通用,但有点尴尬,它基于控制台输出.您可以在Python脚本中向控制台回显一些输出文本.您的父进程需要拾取此文本,然后按照所需的方式对其进行解析和处理.为此,您需要将子进程(在这种情况下为Python脚本)的输出重定向到流,并读取该流的内容.这是通过重定向StandardOutputStandardError流来完成的.请参阅:
http://msdn.microsoft.com/en-us/library/system. diagnostics.process.standardoutput.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. diagnostics.process.standarderror.aspx [ ^ ].

上面引用的两个MSDN帮助页面都显示了如何对代码示例进行重定向.请参阅-它们具有足够的描述性.

—SA
?. Please see:
http://stackoverflow.com/questions/285289/exit-codes-in-python[^].

On the side of your .NET parent process, when the child process is terminated, you can pick up the exit code using the property System.Diagnostics.Process.ExitCode:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx[^].

You can synchronize a thread of your parent process with the termination of your child process using the methods System.Diagnostics.Process.WaitForExit:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

Please note that these methods are blocking, so they are not suitable for calling, say, from a UI thread — you might need to execute all your code using System.Diagnostics.Process from a separate thread, at least in the case if your Python script takes considerable time to complete.

The second approach is more universal but a bit more awkward and is based on console output. You can echo some output text to the console in your Python script. Your parent process needs to pick up this text, parse and process it the way you need. To do that, you need to redirect output from the child process (Python script, in this case) to a stream and read the content of this stream. This is done by redirection of StandardOutput, and, just in case, StandardError streams. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx[^].

Both MSDN help pages referenced above show how to do the redirection on the code samples. Please see — they are descriptive enough.

—SA


这篇关于如何从C#中的python脚本获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆