Select to view content in your preferred language

Calculation string field with python

746
1
09-20-2010 11:24 AM
HolmSeifert
New Contributor
Hallo,

i have a problem with calculating a field with a string variable.
The string variable is the filename1. But the field description doesn´t take take the string value.

Thanks for your help.







PYTHON CODE:

fclist = gp.ListFeatureClasses("*", "Polygon")
for featureclass in fclist:
    name = str(featureclass)
    # setting filenames and pathvars
    filename1 = os.path.splitext(featureclass)[0]
    print filename1
    pathvar1 = ReferenceWorkspace + "\\" + filename1
    filename2 = OutputLocation + "\\" + filename1 + "_Dissolve"
  

    # Dissolve selected attirbute field
    gp.Dissolve_management (featureclass, filename2, DissolveField)
   
    OutputLocation = gp.Workspace
    # Add Field Description and calculate Description with file name
    gp.AddField_management (filename2, "Description", "TEXT","","","30" )
    gp.CalculateField_management (filename2, "Description", 'filename1', "Python","")
0 Kudos
1 Reply
ChrisMathers
Regular Contributor II
Try this:
gp.CalculateField_management (filename2, "Description", filename1, "VB","")

What you were telling the tool is that your expression was the string 'filename1' not the value of the variable filename1. The easiest thing to do in Calculate Field is to do all of your calculations outside the tool syntax and then plug the value in with a variable. Also python code blocks in CF are tricky and can be a mess to debug as well so I try to avoid using them if I can.
0 Kudos