简体   繁体   English

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

[英]Python enumeration dropping the last element of a list

I am working off this article here trying to parse some command line arguments but the script I built keeps dropping the last argument. 我在这里试图解析一些命令行参数,但是我构建的脚本不断删除最后一个参数。

To keep it simple I reproduced the problem like so: 为了保持简单,我重现了这样的问题:

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)

Here is what I get for output: 这是我得到的输出:

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

Where am I going wrong? 我哪里错了?

The colon ( : ) is not a separator, it needs to follow each argument as stated 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 ( ':' ; ie, the same format that Unix getopt() uses). shortopts是脚本想要识别的选项字母串,其中的选项需要一个参数后跟冒号( ':' ;即,与Unix getopt()使用的格式相同)。

Therefore you should change "c:o:e:q" to "c:o:e:q:" 因此,您应该将"c:o:e:q"更改为"c:o:e:q:"

The tutorial you linked to uses it the same way too. 您链接的教程也以相同的方式使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM