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。
6 个解决方案
#1
6
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)
#2
3
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 optparse module which has been in the standard library since 2.3.
替换与您的(电子表格?)情况相关的位。这是列表理解。您还可以调查自2.3以来已在标准库中的optparse模块。
#3
2
I would recommend taking a look at Python's optparse module. It's a nice helper to parse sys.argv
.
我建议看一下Python的optparse模块。这是解析sys.argv的好帮手。
#4
1
argparse is another powerful, easy to use module that parses sys.argv for you. Very useful for creating command line scripts.
argparse是另一个功能强大,易于使用的模块,可以为您解析sys.argv。对于创建命令行脚本非常有用。
#5
0
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)
#6
0
# 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
]
#1
6
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)
#2
3
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 optparse module which has been in the standard library since 2.3.
替换与您的(电子表格?)情况相关的位。这是列表理解。您还可以调查自2.3以来已在标准库中的optparse模块。
#3
2
I would recommend taking a look at Python's optparse module. It's a nice helper to parse sys.argv
.
我建议看一下Python的optparse模块。这是解析sys.argv的好帮手。
#4
1
argparse is another powerful, easy to use module that parses sys.argv for you. Very useful for creating command line scripts.
argparse是另一个功能强大,易于使用的模块,可以为您解析sys.argv。对于创建命令行脚本非常有用。
#5
0
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)
#6
0
# 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
]