for python中的语句

Tyl*_*ler 1 python

当一个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        
Run Code Online (Sandbox Code Playgroud)

我试图打印出来例如:

-j 90 -az 0(然后是什么blah包含)blah是第54行.第54行打印出大量信息.大多数话.我想打印出54行直接显示在屏幕上的内容

-j 90 -az 0

@Robbie:第39行

blah = outputlist[53]
Run Code Online (Sandbox Code Playgroud)

Indexerror:列表索引超出范围

@Robbie又来了.谢谢你的帮助,抱歉找麻烦的人......

我甚至尝试输入outputlist [2]并且它给出了同样的错误:/

Bri*_*unt 6

我忍不住要清理一下.

# 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]
Run Code Online (Sandbox Code Playgroud)

您需要定义cmd - 现在它正在尝试执行类似"-j 90 -b 288"的操作.我认为你想要像cmd ="program_name -j 90 -b 288"这样的东西.

不知道这是否能回答你的问题,但我希望它能让人深思.