加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python枚举删除列表的最后一个元素

发布时间:2020-12-20 11:09:39 所属栏目:Python 来源:网络整理
导读:我正在处理这篇文章 here尝试解析一些命令行参数,但我构建的脚本不断删除最后一个参数. 为了保持简单,我重现了这样的问题: import getoptargv = ["-c","config","-o","hello","-e","fu bar","-q","this is a query"]opts,args = getopt.getopt(argv,"c:o:e:
我正在处理这篇文章 here尝试解析一些命令行参数,但我构建的脚本不断删除最后一个参数.

为了保持简单,我重现了这样的问题:

import getopt

argv = ["-c","config","-o","hello","-e","fu bar","-q","this is a query"]
opts,args = getopt.getopt(argv,"c:o:e:q",["cfile=","ofile=","entry=","query="])

for opt,arg in opts:
    print(opt,arg)

这是我得到的输出:

-c config
-o hello
-e fu bar
-q

我哪里错了?

解决方法

冒号(:)不是分隔符,它需要遵循 in the docs所述的每个参数:

shortopts is the string of option letters that the script wants to recognize,with options that require an argument followed by a colon (':'; i.e.,the same format that Unix getopt() uses).

因此,您应该将“c:o:e:q”更改为“c:o:e:q:”

您链接的教程也以相同的方式使用它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读