如何将变量传递给Python脚本?
我知道可以通过命令行来实现这个,但我需要传递至少10个变量,使用命令行的话会涉及太多编程,因为这些变量可能会被传递,也可能不会。
其实我已经做了一个应用程序,界面部分是用VB写的,脚本部分是用Python写的。我需要把变量传递给Python,类似于它的关键字参数,比如,x = val1,y = val2。有没有什么办法可以做到这一点?
6 个回答
3
因为你在Windows上用VB工作,所以值得提一下 IronPython 可能是一个选择。VB和IronPython都可以通过.NET进行互动,你可以把你的脚本打包成一个程序集,然后暴露出一个函数,使用需要的参数来调用它。
6
你可以做一些有趣的事情,比如这样调用它:
thepyscript.py "x = 12,y = 'hello world', z = 'jam'"
然后在你的脚本里面,
解析可以这样做:
stuff = arg[1].split(',')
for item in stuff:
exec(item) #or eval(item) depending on how complex you get
#Exec can be a lot of fun :) In fact with this approach you could potentially
#send functions to your script.
#If this is more than you need, then i'd stick w/ arg/optparse