lemanlake

Problem when making a loop to calculate fields

Discussion created by lemanlake on Dec 30, 2011
Latest reply on Dec 30, 2011 by lemanlake
Hello everyone,

I'm new in Python and began the writing of a script (I work on ArcGIS 9).
This script consists in adding new columns to a layer and calculating values into these new columns. To make this script more "interesting" I decided to build loops, described in the code below :

import sys, string, os, arcgisscripting
gp = arcgisscripting.create()
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")


# Variables in local
radars_shp = "C:\\_basedata_arcgis\\exemple1\\radars.shp"
radars_shp__1_ = "C:\\_basedata_arcgis\\exemple1\\radars.shp"
radars_shp__2_ = "C:\\_basedata_arcgis\\exemple1\\radars.shp"

# Adding new columns
new_fields=["rad", "inc", "azi"]
while 1:
    for i in range(len(new_fields)): 
        Field_Name = new_fields[i]
        try:
            gp.AddField_management(radars_shp, Field_Name, "DOUBLE", "")
            print "Adding field", i+1, "is ok"
        except:
            print "KO Add"
    break


# Calculate fields
new_calculate=["[sol] / 4", "[sol] / 3", "[sol] / 2"]
while 1:
    for j in range(len(new_calculate)):
        Calcul = new_calculate[j]
        try:
            gp.CalculateField_management(radars_shp__1_, Field_Name, Calcul, "VB", "")
            print "Calculate field", j+1, "is ok"
        except:
            print "KO Calculate"
    break


The "adding new columns" loop works perfectly. But the "calculate fields" one is wrong. Actually the calculation has only filled the field "azi" whereas there is nothing in the two others "rad" and "inc".
I tried many things to correct the loop but I desperately have the same result !

Have you got ideas to make it right?

Thanks!
Julien

Outcomes