python命令行传入参数方式

Python016

python命令行传入参数方式,第1张

如果在运行python脚本时需要传入一些参数,可以使用如下两种方式

sys模块是python常用的一个模块,封装了python解释器相关的数据 . sys.argv 封装了传入的参数数据。

使用 sys.argv 接收上面第一个命令中包含的参数方式

parser.add_argument 方法的 type 参数理论上可以是任何合法的类型, 但有些参数传入格式比较麻烦,例如list,所以一般使用 bool , int , str , float 这些基本类型就行了,更复杂的需求可以通过 str 传入,然后手动解析。 bool 类型的解析比较特殊,传入任何值都会被解析成 True ,传入空值时才为`False

具体请参考API文档: https://docs.python.org/2/library/argparse.html

pythonsql存储过程传出参数有两种模式。

1、普通传递参数。输入以下代码:conn=MySQLdb.connect(user=root,passwd=123456,host=192.168.101.23,db=cmdb)。orange_id=98。sql=select×fromorangewhereid=%s%orange_id。cursor=conn.cursor(sql)。cursor.execute()。

2、使用字典dict类型传递参数。输入以下代码:sql=select×fromorangewhere%(color)s,%(weight)s。values={color:yellow,weight:30}。cursor.execute(sql,values)。