从C#调用Python应用程序/脚本

我正在构建一个ASP.NET MVC(C#)站点,我想在其中实现STV( Single Transferable Vote )投票 。 我之前使用OpenSTV进行投票,取得了巨大成功,但我从未以编程方式使用它。

OpenSTV Google Code项目提供了一个Python脚本 ,允许从其他应用程序使用OpenSTV:

import sys sys.path.append("path to openstv package") from openstv.ballots import Ballots from openstv.ReportPlugins.TextReport import TextReport from openstv.plugins import getMethodPlugins (ballotFname, method, reportFname) = sys.argv[1:] methods = getMethodPlugins("byName") f = open(reportFname, "w") try: b = Ballots() b.loadUnknown(ballotFname) except Exception, msg: print >> f, ("Unable to read ballots from %s" % ballotFname) print >> f, msg sys.exit(-1) try: e = methods[method](b) e.runElection() except Exception, msg: print >> f, ("Unable to count votes using %s" % method) print >> f, msg sys.exit(-1) try: r = TextReport(e, outputFile=f) r.generateReport(); except Exception, msg: print >> f, "Unable to write report" print >> f, msg sys.exit(-1) f.close() 

有没有办法让我从C#ASP.NET MVC站点进行这样的Python调用?

如果是这样,怎么样?

提前致谢!

这是一个很好的例子,说明如何从C#调用IronPython,包括传递参数和返回结果; 当然,您必须将该代码转换为函数,并将ballotFnamereportFname作为其参数。

最好的方法可能是使用IronPython。 请参阅此答案以获取起点。