我想从ruby方法运行python脚本。我在Rails应用程序中将此方法作为rake任务运行。我使用的方案中提到here:如何获得从红宝石方法执行的python脚本的输出
def create
path = File.expand_path('../../../../GetOrders', __FILE__)
output = `"python2 " + path + "/parse.py"`
print output
str = JSON.parse(output)
print str
end
编辑:这工作:
output = `python2 #{path}/parse.py`
EDIT2: 使用Python脚本我试图词典列表传递给红宝石功能。 python脚本看起来是这样的:
import xml.etree.ElementTree as ET
import json
def parse():
tree = ET.parse('response.xml')
root = tree.getroot()
namespaces = {'resp': 'urn:ebay:apis:eBLBaseComponents'}
order_array = root.find("resp:OrderArray", namespaces=namespaces)
detailsList = []
for condition:
details["key1"] = value1
details["key2"] = value2
detailsList.append(details)
output = json.dumps(detailsList)
return output
print parse()
有人可以解释我做错了什么,我该如何解决这个问题。感谢
你可以直接在命令行上运行吗? –
@thorstenmüller:是的,python脚本在命令行 – nish
['Kernel#''](http://ruby-doc.org/core-2.1.0/Kernel.html#method-i-60)不要把它的论点看成是字符串。 –