python sorted,python getopt使用_python getopt使用

 2023-11-09 阅读 47 评论 0

摘要:python中 getopt 模塊,該模塊是專門用來處理命令行參數的函數getopt(args, shortopts, longopts = [])參數args一般是sys.argv[1:]python sorted,shortopts 短格式 (-)longopts 長格式(--)命令行中輸入:python test.py -i 127.0.0.1 -p 80 55 66python tes

python中 getopt 模塊,

該模塊是專門用來處理命令行參數的

函數getopt(args, shortopts, longopts = [])

參數args一般是sys.argv[1:]

python sorted,shortopts 短格式 (-)

longopts 長格式(--)

命令行中輸入:

python test.py -i 127.0.0.1 -p 80 55 66

python test.py –ip=127.0.0.1 –port=80 55 66

python 字符串。下面的代碼:

try:

options,args = getopt.getopt(sys.argv[1:],”hp:i:”,[“help”,”ip=”,”port=”])

except getopt.GetoptError:

sys.exit()

python類?for name,value in options:

if name in (“-h”,”–help”):

usage()

if name in (“-i”,”–ip”):

print ‘ip is—-‘,value

python爬蟲教程。if name in (“-p”,”–port”)

print 'port is----',value

options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])

“hp:i:”

短格式 — h 后面沒有冒號:表示后面不帶參數,p:和 i:后面有冒號表示后面需要參數

python編程?[“help”,”ip=”,”port=”]

長格式 — help后面沒有等號=,表示后面不帶參數,其他三個有=,表示后面需要參數

返回值 options 是個包含元祖的列表,每個元祖是分析出來的格式信息,比如 [(‘-i’,’127.0.0.1′),(‘-p’,’80’)] ;

args 是個列表,包含那些沒有‘-’或‘–’的參數,比如:[’55’,’66’]

注意:定義命令行參數時,要先定義帶’-‘選項的參數,再定義沒有‘-’的參數

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/171027.html

发表评论:

猜你喜欢

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息