3

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?

0

1 Answer 1

11

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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