python 如何测试多个命令行参数 (sys.argv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1643643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:47:23  来源:igfitidea点击:

How to test for multiple command line arguments (sys.argv

pythonexcelloops

提问by jrara

I want to test againts multiple command line arguments in a loop

我想在循环中测试多个命令行参数

> python Read_xls_files.py group1 group2 group3

No this code tests only for the first one (group1).

没有此代码仅测试第一个(group1)。

hlo = []
for i in range(len(sh.col_values(8))):
   if sh.cell(i, 1).value == sys.argv[1]:
      hlo.append(sh.cell(i, 8).value)

How should I modify this that I can test against one, two or all of these arguments? So, if there is group1 in one sh.cell(i, 1), the list is appended and if there is group1, group2 etc., the hlo is appended.

我应该如何修改它,以便我可以针对这些参数中的一个、两个或所有参数进行测试?因此,如果在一个 sh.cell(i, 1) 中有 group1,则附加列表,如果有 group1、group2 等,则附加 hlo。

回答by Stephen Doyle

You can iterate over sys.argv[1:], e.g. via something like:

你可以迭代sys.argv[1:],例如通过类似的东西:

for grp in sys.argv[1:]:
  for i in range(len(sh.col_values(8))):
   if sh.cell(i, 1).value == grp:
      hlo.append(sh.cell(i, 8).value)

回答by Caleb Hattingh

outputList = [x for x in values if x in sys.argv[1:]]

Substitute the bits that are relevant for your (spreadsheet?) situation. This is a list comprehension. You can also investigate the optparsemodule which has been in the standard library since 2.3.

替换与您的(电子表格?)情况相关的位。这是一个列表理解。您还可以研究自 2.3 以来一直在标准库中的optparse模块。

回答by Bartek

I would recommend taking a look at Python's optparsemodule. It's a nice helper to parse sys.argv.

我建议看看 Python 的optparse模块。是解析的好帮手sys.argv

回答by mavnn

argparseis another powerful, easy to use module that parses sys.argv for you. Very useful for creating command line scripts.

argparse是另一个功能强大且易于使用的模块,可为您解析 sys.argv。对于创建命令行脚本非常有用。

回答by John Machin

# First thing is to get a set of your query strings.
queries = set(argv[1:])
# If using optparse or argparse, queries = set(something_else)
hlo = []
for i in range(len(sh.col_values(8))):
    if sh.cell(i, 1).value in queries:
        hlo.append(sh.cell(i, 8).value)

=== end of answer to question ===

=== 问题回答结束 ===

Aside: the OP is using xlrd ... here are a couple of performance hints.

旁白:OP 正在使用 xlrd ...这里有一些性能提示。

Doesn't matter too much with this simple example, but if you are going to do a lot of coordinate-based accessing of cell values, you can do better than that by using Sheet.cell_value(rowx, colx) instead of Sheet.cell(rowx, colx).value which builds a Cell object on the fly:

这个简单的例子没有太大关系,但如果你要对单元格值进行大量基于坐标的访问,你可以通过使用 Sheet.cell_value(rowx, colx) 而不是 Sheet.cell 来做得更好(rowx, colx).value 动态构建一个 Cell 对象:

queries = set(argv[1:])
hlo = []
for i in range(len(sh.nrows)): # all columns have the same size
    if sh.cell_value(i, 1) in queries:
        hlo.append(sh.cell_value(i, 8))

or you could use a list comprehension along with the Sheet.col_values(colx) method:

或者您可以将列表理解与 Sheet.col_values(colx) 方法一起使用:

hlo = [
    v8
    for v1, v8 in zip(sh.col_values(1), sh.col_values(8))
    if v1 in queries
    ]

回答by Graeme Perrow

I believe this would work, and would avoid iterating over sys.argv:

我相信这会起作用,并且会避免迭代 sys.argv:

hlo = []
for i in range(len(sh.col_values(8))):
   if sh.cell(i, 1).value in sys.argv[1:]:
      hlo.append(sh.cell(i, 8).value)