Tyler
Tyler

Reputation: 4007

for statement in python

When an exe file is run it prints out some stuff. I'm trying to run this on some numbers below and print out line 54 ( = blah ). It says process isn't defined and I'm really unsure how to fix this and get what I want printed to the screen. If anyone could post some code or ways to fix this thank you so very much!

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        

I am trying to print out for example:

-j 90 -az 0 (then what blah contains) blah is line 54. Line 54 prints out a lot of information. Words mostly. I want to print out what line 54 says to the screen right after

-j 90 -az 0

@ Robbie: line 39

blah = outputlist[53]

Indexerror: list index out of range

@ Robbie again. Thanks for your help and sorry for the trouble guys...

I even tried putting in outputlist[2] and it gives same error :/

Upvotes: 1

Views: 588

Answers (4)

Brian M. Hunt
Brian M. Hunt

Reputation: 83798

I can't help but clean that up a little.

# 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]

You need to define cmd -- right now it's trying to execute something like " -j 90 -b 288". I presume you want something like cmd = "program_name -j 90 -b 288".

Don't know if that answers your question at all, but I hope it gives food for thought.

Upvotes: 6

S.Lott
S.Lott

Reputation: 391828

process isn't defined because your statements are out of order.

    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)

cannot possibly work. process on the first line, is undefined.

Upvotes: 1

Hank Gay
Hank Gay

Reputation: 71939

The following line

outputstring = process.communicate()[0]

calls the communicate() method of the process variable, but process has not been defined yet. You define it later in the code. You need to move that definition higher up.

Also, your variable names (j,k, and jk) are confusing.

Upvotes: 2

eduffy
eduffy

Reputation: 40224

Are you sure this is right

cmd =  ' -j ' + str(el) + ' -jk ' + str(az) + ' blah '

Where's your executable?

Upvotes: 2

Related Questions