我想运行mysql命令并将其输出设置为我的python脚本中的变量。

这是我要运行的shell命令:

$ mysql my_database --html -e "select * from limbs" | ./script.py

这是python脚本:
#!/usr/bin/env python

import sys

def hello(variable):
    print variable

我如何在python脚本中接受变量并将其打印输出?

最佳答案

您需要从stdin中读取内容以检索python脚本中的数据,例如

#!/usr/bin/env python

import sys

def hello(variable):
    print variable

data = sys.stdin.read()
hello(data)

如果您要做的只是从mysql数据库中获取一些数据,然后使用Python对其进行操作,我将跳过将其管道传递到脚本中,而仅使用the Python MySql module进行SQL查询。

07-24 09:46
查看更多