Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to add (ie "Author", "LastUpdated") to feature classes within a gdb and then populate each of those fields. I have been able to get my code to run properly for the numerical field but I cannot get it to run for the text field.

    fcList = arcpy.ListFeatureClasses()
    fieldName1 = "Author"
    expression1 = "John Doe"

    for fc in fcList:    

            arcpy.AddField_management(fc,fieldName1, "TEXT")

            arcpy.CalculateField_management(fc, fieldName1, expression1, "PYTHON")

My code returns this error:

ExecuteError: ERROR 000539: <type 'exceptions.SyntaxError'>: unexpected EOF while parsing (<expression>, line 1)
Failed to execute (CalculateField).

How can I fix this?

share|improve this question
up vote 7 down vote accepted

You have to include quotes within your expression string. Try this:

fcList = arcpy.ListFeatureClasses()
    fieldName1 = "Author"
    expression1 = "\"John Doe\""

    for fc in fcList:    

            arcpy.AddField_management(fc,fieldName1, "TEXT")

            arcpy.CalculateField_management(fc, fieldName1, expression1)

By adding in escaped double quotes, your field will be sent the string "John Doe" instead of John Doe without quotes. For text fields, the quotes are required.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.