Select to view content in your preferred language

CalculateField error

2813
3
Jump to solution
08-27-2014 05:25 AM
Zeke
by
Honored Contributor

I'm getting an ExecuteError, error 000539, in the code below when Calculating the field. The error appears to be on the second instance of fc, fc.capitalize(), because when I take out the capitalize(), I get the same error but without the capitalized name. I can't figure out what the problem is. Any ideas? Thanks.

# code

fds = 'RawFramework'

    newfield = 'CountyName'

    lstRawFCs = arcpy.ListFeatureClasses(None, 'All', fds)

    for fc in lstRawFCs:

        try:

            arcpy.AddField_management(fc, newfield, 'TEXT', field_length = 25, field_alias = 'County Name')

            arcpy.CalculateField_management(fc, newfield, fc.capitalize(), 'PYTHON_9.3')

        except Exception as e:

            print(e.message, e.__class__)

# sample error

(CalculateField).\n', <class 'arcgisscripting.ExecuteError'>)

('ERROR 000539: Error running expression: St_clair \nTraceback (most recent call last):\n  File "<expression>", line 1, in <module>\nNameError: name \'San_Diego\' is not defined\n\nFailed to execute (CalculateField).\n', <class 'arcgisscripting.ExecuteError'>)

0 Kudos
1 Solution

Accepted Solutions
Zeke
by
Honored Contributor

Solved. Needed quotes around the value.

value = '"' + fc.capitalize() + '"'

arcpy.CalculateField_management(fc, newfield, value, 'PYTHON_9.3')

View solution in original post

3 Replies
Zeke
by
Honored Contributor

Solved. Needed quotes around the value.

value = '"' + fc.capitalize() + '"'

arcpy.CalculateField_management(fc, newfield, value, 'PYTHON_9.3')

curtvprice
MVP Esteemed Contributor

You are correct, string expressions in a python expression string need quotes.

Single and double are interchangeable in Python (although the pairs must match). I try to use double as the "outside" quotes just for consistency as this seems to match what they do in the documentation and I find it easier to see.

I usually use string formatting of Python, SQL, and string expressions to make my code easier to read:

value = "'{0}'".format(fc.capitalize())

arcpy.CalculateField_management(fc, newfield, value, 'PYTHON_9.3')

Posting Code blocks in the new GeoNet

RachelGomez
Emerging Contributor

Try importing your shapefile into a GDB as a table before preforming a Join or Relate. This can be completed many ways, such as from Catalog, right click on your GDB and select Import > Table(s). Then perfor the join on this table in your GDB not the CSV.

Regards,

Rachel Gomez

0 Kudos