如何从python脚本返回数据
我在返回以下输出的python脚本中有这段代码如何从python脚本返回数据,python,output,Python,Output,我在返回以下输出的python脚本中有这段代码 dir = ["/var/www", "/usr/share"] for dir_name in dirs: if os.path.exists(dir_name): cmd = ["drush", "st"] rc, out, err = module.run_command(cmd, cwd=dir_name) if rc == 0:
dir = ["/var/www", "/usr/share"]
for dir_name in dirs:
if os.path.exists(dir_name):
cmd = ["drush", "st"]
rc, out, err = module.run_command(cmd, cwd=dir_name)
if rc == 0:
info = out.splitlines()
version = info[0].strip().split(':')[-1].strip()
return {'version': version}
输出:
PHP executable : /usr/bin/php
PHP configuration : /etc/php5/cli/php.ini
PHP OS : Linux
Drush version : 6.2.0
Drush configuration :
Drush alias files :
如何获取版本变量返回的版本6.2.0?我们可以这样做吗
version.partition('version')[2].split('\n')[0].split(':')[1].strip()
我冒昧地考虑了一下
version = '''PHP executable : /usr/bin/php
PHP configuration : /etc/php5/cli/php.ini
PHP OS : Linux
Drush version : 6.2.0
Drush configuration :
Drush alias files : '''
这应该是可行的,但是最好使用正则表达式。使用正则表达式,可能是在python中,也可能是在grep实用程序中?我很困惑,代码看起来正好返回版本。你的问题是什么?version=info[0]。strip.split':'[-1]。strip您的意思是来自外部程序还是在python脚本中?是的,它的ansible。脚本返回以下drupal:{version:/usr/bin/php