从shell命令管道输出到python脚本
发布时间:2020-12-15 18:56:16 所属栏目:安全 来源:网络整理
导读:我想运行一个mysql命令,并将其输出设置为我的python脚本中的一个变量. 这是我要运行的shell命令: $mysql my_database --html -e "select * from limbs" | ./script.py 这是python脚本: #!/usr/bin/env pythonimport sysdef hello(variable): print variabl
我想运行一个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查询. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |