当一个 exe 文件运行时,它会打印出一些东西。我试图在下面的一些数字上运行它并打印出第 54 行(= blah)。它说过程未定义,我真的不确定如何解决这个问题并将我想要的内容打印到屏幕上。如果有人可以发布一些代码或解决此问题的方法,非常感谢!
for j in ('90','52.62263','26.5651','10.8123'):
if j == '90':
k = ('0',)
elif j == '52.62263':
k = ('0', '72', '144', '216', '288')
elif j == '26.5651':
k = (' 324', ' 36', ' 108', ' 180', ' 252')
else:
k = (' 288', ' 0', ' 72', ' 144', ' 216')
for b in k:
outputstring = process.communicate()[0]
outputlist = outputstring.splitlines()
blah = outputlist[53]
cmd = ' -j ' + str(j) + ' -b ' + str(b) + ' blah '
process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
print cmd
例如,我正在尝试打印出来:
-j 90 -az 0 (然后是 blah 包含的内容) blah 是第 54 行。第 54 行打印出很多信息。主要是单词。我想在
之后立即打印出第 54 行对屏幕显示的内容-j 90 -az 0
@罗比:第 39 行
blah = outputlist[53]
Indexerror: 列表索引超出范围
再次@罗比。感谢您的帮助,很抱歉给您带来麻烦...
我什至尝试输入 outputlist[2],但它给出了同样的错误:/
请您参考如下方法:
我忍不住稍微清理了一下。
# aesthetically (so YMMV), I think the code would be better if it were ...
# (and I've asked some questions throughout)
j_map = {
90: [0], # prefer lists [] to tuples (), I say...
52.62263: [0, 72, 144, 216, 288],
26.5651: [324, 36, 108, 180, 252],
10.8123: [288, 0, 72, 144, 216]
}
# have a look at dict() in http://docs.python.org/tutorial/datastructures.html
# to know what's going on here -- e.g. j_map['90'] is ['0',]
# then the following is cleaner
for j, k in j_map.iteritems():
# first iteration j = '90', k=[0]
# second iteration j = '52.62263'', k= [0,...,288]
for b in k:
# fixed the ordering of these statements so this may actually work
cmd = "program_name -j %f -b %d" % (j, b)
# where program_name is the program you're calling
# be wary of the printf-style %f formatting and
# how program_name takes its input
print cmd
process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
outputlist = outputstring.splitlines()
blah = outputlist[53]
您需要定义 cmd——现在它正在尝试执行类似“-j 90 -b 288”的操作。我猜你想要像 cmd = "program_name -j 90 -b 288" 这样的东西。
不知道这是否完全回答了您的问题,但我希望它能引起思考。
