Hello, I would like to execute a double for-loop in python. I use the python interface with ArcGIS (hence the gp. prefixes and weird functions). Here is my double-for loop attempt but I can't execute the code due to 'invalid syntax.' If I remove the bottom loop, the code runs fine. I have two Intro Python books that do not go into syntax for double for-loops. Any suggestions?
Thank you,
AnnetteM

P.s Here's the porblem part of the code, the second half of this loop. However, when I preview the post, the indents don't show! Does the invalid syntax have to do with indenting?, if so, it would only involve the second half. The second for loop is indented from the first.

for tiffile in runlist:
basename0=os.path.basename(tiffile[0])
basename1=basename0[0:9]
InRaster=tiffile[0]
InRast.append(InRaster)

for zone in zonelist
basenamez=os.path.basename(zone[0])
string_zonenum=basenamez[3]
Output_raster = extrDIR + basename1 + string_zonenum
# Process: Extract by Mask...
gp.ExtractByMask_sa(InRaster, zone, Output_raster)
quantVARNAME.append(Output_raster) # Add to the list

Recommended Answers

All 9 Replies

Since you are not showing any indentations, your question will be hard to answer.

For code tags info see:
http://www.daniweb.com/forums/announcement114-3.html

for tiffile in runlist:
    basename0=os.path.basename(tiffile[0])
    basename1=basename0[0:9]
    InRaster=tiffile[0]
    InRast.append(InRaster)

    for zone in zonelist
        basenamez=os.path.basename(zone[0])
        string_zonenum=basenamez[3]       
        Output_raster = extrDIR +  basename1 + string_zonenum
        # Process: Extract by Mask...
        gp.ExtractByMask_sa(InRaster, zone, Output_raster)
        quantVARNAME.append(Output_raster) # Add to the list

I am extracting 9 zones of data (second loop) from each of 6 data sets as rasters or maps (first loop). I use the first loop to get the basename from each raster to make the first part of the file name for the output. The second part of the file name is the zone number, and the final file name is defined in the second loop as a combo between each raster and zone number. Also, I am extracting each zone of data from each map in the second loop.

There is a colon missing at the end of for zone in zonelist . Add : . Also, when the interpreter says that there is an error, it prints a wonderful thing called a exception traceback. This is very useful to helpers, so when you have an error, please post the traceback (between code tags!).

Gribouillis, I must say, you are Sherlock Holmes in disguise! Thank you for another case solved. I'm working in PythonWin, and interestingly, that 'invalid syntax' error gave no traceback message in the command window. It just gave a message at the bottom of the gray PythonWin frame right where it says,"running the code file..." or something similar to that.

Gribouillis, I must say, you are Sherlock Holmes in disguise! Thank you for another case solved. I'm working in PythonWin, and interestingly, that 'invalid syntax' error gave no traceback message in the command window. It just gave a message at the bottom of the gray PythonWin frame right where it says,"running the code file..." or something similar to that.

If PythonWin does that, then it should be considered a bug in PythonWin. It means that when your program fails, you should try to run it outside of PythonWin to see the traceback.

It just gave a message at the bottom of the gray PythonWin frame right where it says,"running the code file..." or something similar to that

Get a better editor than PythonWin.
Pyscripter is good.
Paste your kode inn,and you se a red line mark under line 7 for your code.
It also give you and postion(25) if you try to run the code.
Because at postion(25) there should be a : as Gribouillis pointet out.

If PythonWin does that, then it should be considered a bug in PythonWin. It means that when your program fails, you should try to run it outside of PythonWin to see the traceback.

I wonder if I should report the bug to "PythonWin"ners?

Get a better editor than PythonWin.
Pyscripter is good.
Paste your kode inn,and you se a red line mark under line 7 for your code.
It also give you and postion(25) if you try to run the code.
Because at postion(25) there should be a : as Gribouillis pointet out.

Wow, thanks for that tip!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.