Adding a sring value to a field with python

456
4
06-29-2010 12:10 PM
HolmSeifert
New Contributor
Hi everyone,

i want to assign a variable , a string, to a field. Therefore i have created with python a field and want now assigning a value to this field,
But i get only this failure message back.

<class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: name2 <type 'exceptions.NameError'>: name 'name2' is not defined
Failed to execute (CalculateField).

The python code is listed above.


for raster in rasterlist:
    name = str(raster) + "_WGS84"
    name2 = str(raster) + "test"
    gp.MosaicToNewRaster_management (raster, OutputLocation, name, coordinatesystem, pixeltype, cellsize, bandnumber, "", "")
    pathvar1 = OutputLocation + "\\" + name
    gp.AddField_management (pathvar1, "Name", "TEXT")
    gp.CalculateField_management (pathvar1, "Name", 'name2' , "PYTHON")


Holm
0 Kudos
4 Replies
LanceShipman
Esri Regular Contributor
Hi everyone,

i want to assign a variable , a string, to a field. Therefore i have created with python a field and want now assigning a value to this field,
But i get only this failure message back.

<class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: name2 <type 'exceptions.NameError'>: name 'name2' is not defined
Failed to execute (CalculateField).

The python code is listed above.


for raster in rasterlist:
    name = str(raster) + "_WGS84"
    name2 = str(raster) + "test"
    gp.MosaicToNewRaster_management (raster, OutputLocation, name, coordinatesystem, pixeltype, cellsize, bandnumber, "", "")
    pathvar1 = OutputLocation + "\\" + name
    gp.AddField_management (pathvar1, "Name", "TEXT")
    gp.CalculateField_management (pathvar1, "Name", 'name2' , "PYTHON")


Holm


'name2' should be name2.
0 Kudos
HolmSeifert
New Contributor
Hallo

Ok i have chaned th e name.
python is running but the field is not calculated
i thing name is defined as string automatically?

O do i need a expressionn?

here is the code:


for raster in rasterlist:
    name = str(raster) + "_WGS84"
    name2 = name
    gp.MosaicToNewRaster_management (raster, OutputLocation, name, coordinatesystem, pixeltype, cellsize, bandnumber, "", "")
    pathvar1 = OutputLocation + "\\" + name
    gp.AddField_management (pathvar1, "Name", "TEXT")
    expression = str(raster)
    gp.CalculateField_management (pathvar1, "Name", name2, "PYTHON", "")
0 Kudos
HolmSeifert
New Contributor
sorry

I have written a little bit to fast

thanks
Holm
0 Kudos
ValentinaBoycheva
New Contributor III
Same error message for me. Finally made it work as follows in the command window:

CalculateField SouthPoles junk x PYTHON "x = 'SouthPoles'"

SouthPoles is the name of the feature class, junk is the field, x is the variable and the last expression is the code where the variable x is being assigned a string value, in this case the feature class name.

================================
In the Python script the syntax is:
(fc is the feature class)

fcname = str(fc)
gp.CalculateField(fc, newField, "fcname", "PYTHON", "#")

Note how the parameter fcname is surrounded by quotes - otherwise it won't calculate.

ArcMap 9.3, Python 2.5.1

0 Kudos