1

I have the following python input. The user will enter a value with a dash always. E.g. 2015-0001

Var1 = raw_input("Enter the number with dash: ")
arcpy.CalculateField_management("all_reports.gdb\\joined", "YearNo", Var1, "PYTHON_9.3", "")

"YearNo" is a text field. If I do "print type(Var1)" it tells me that it is a string.

When I look at my YearNo field is shows 1972. It is doing a subtraction of the numbers rather than keeping a string together.

Is there a way around this?

4

The problem is that CalculateField takes as the expression a string argument that is a python expression. The string is being evaluated inside CalculateField, so you need to encapsulate the value in quotes:

arcpy.CalculateField_management("all_reports.gdb\\joined", "YearNo", "'" + Var1 + "'", "PYTHON_9.3", "")
-1

You raise an interesting point with this problem; almost a sort of semantic bug in the Field Calculator. The Field Calculator expression argument (Var1, a string) is being read in a sort of strictly literal way as an expression in the same way you'd expect to see an expression written in the Field Calculator GUI.

So, if we add str() to the expression argument we should get the desired effect:

Var1 = raw_input("Enter the number with dash: ")
arcpy.CalculateField_management("all_reports.gdb\\joined", "YearNo", str(Var1), "PYTHON_9.3", "")
  • No sorry that didn't work. – Andrew Jan 15 '15 at 15:52
  • Whoops - sorry Andrew - go with Martin Wilkerson's answer, I'll pay more attention next time. – Jim Jan 15 '15 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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