试图找出在python中编写此代码的最佳方法。我明白为什么我的代码无法正常工作,只是无法确定解决问题的正确方法。如果VER多于一个,测试将会失败,它不会被分成单个变量。Python,变量或列表中的列表
我想有一个程序打印出基于标志下列内容:所有的,开放式,封闭式(仅当拉特匹配)(仅LVL匹配),NA(仅当所有lvls是NA)
这是我到目前为止有:
#!/usr/bin/python
import getopt, sys
flago='' #show open tickets
flagl='' #show out of lvl tickets
flagc='' #show closed tickets
flaga='' #show all
fname=''
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'olca')
for opt, arg in options:
if opt in ('-o'):
flago = True
elif opt in ('-l'):
flagl = True
elif opt == '-c':
flagc = True
elif opt == '-a':
flaga = True
# fname = remainder[0]
#only show tickets if lvl matches regardless of status
reqlvls = ("lvl1", "lvl2", "lvl3")
#loop through a file with following variables
# number, status(open/closed), lvl#, comments
file = open ('test.conf')
for line in file:
fields = line.strip().split(":")
NUM=fields[0]
STAT=fields[1]
VER=fields[2]
COMM=fields[3]
#print all
if flaga:
print NUM, STAT, VER, COMM
#show open
elif flago:
# VER is messing up if it is set to VER:[lvl1, lvl3]
# Need to iterate check open and iterate over VER
if STAT == "open" and VER in reqlvls:
print NUM, STAT, VER, COMM
#show NA
elif flagl:
if VER not in reqlvls:
print NUM, STAT, VER, COMM
#hide if not reqlvl
elif flagc:
if STAT == "closed" and VER in reqlvls:
print NUM, STAT, VER, COMM
这里是测试文件:$猫test.conf:
10:open:lvl1:"should show w/ -o"
11:open:lvl5:"should not show w/ -o, NA b/c of lvl"
12:open:lvl3, lvl5:"should w/ -o, req lvl > na lvl"
13:open:lvl1, lvl3:"should w/ -o"
14:open:lvl4, lvl5:"should not w/ -o NA b/c of lvl"
20:closed:lvl2:"should show closed"
21:closed:lvl5:"should not show w/ -c, NA b/c of lvl"
22:closed:lvl3, lvl5:"should w/ -c, req lvl > na lvl"
23:closed:lvl1, lvl3:"should w/ -c"
24:closed:lvl4, lvl5:"should not w/ -c NA b/c of lvl"
这里是输出:$
./test.py -a
10 open lvl1 "should show w/ -o"
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
20 closed lvl2 "should show closed"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"
$ ./test.py -o #should show 10,12,13
10 open lvl1 "should show w/ -o"
$ ./test.py -c #should show 20,22,23
20 closed lvl2 "should show closed"
$ ./test.py -l #should show 11,14,21,24
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"
我一直在使用这样的上市功能的尝试:
def checkreq(VER):
for s in reqlvls:
for item in VER:
if item in s:
print 'LVL: ', s
return s
然后改变:
elif flago:
if STAT == "open" and checkreq(VER):
但是,这并不正确的工作之一。
从 Check if list item contains items from another list
太多的代码,并在此输出。将其简化为一个简单的例子。 – 2014-09-29 14:42:29
只是想确保我得到了可能需要的一切 – user3699853 2014-09-29 15:48:42