-1

I am trying to change VB expression of CalculateField_management to Python expression. What I am trying to achieve is to adding suitability rank values to new fields for a dbf created out of raster data. ArcGIS server only accept Python expression so I have to change it. However, the Python expression fails with the following error message.

VB expressions:

arcpy.CalculateField_management(first_stat_table, "POLY_VAL", "([VALUE] - Right([VALUE] , 3)) / 1000", "VB", "")
arcpy.CalculateField_management(first_stat_table, "LAND_RANK", "Right([VALUE] , 3)", "VB", "")

Python expressions:

arcpy.CalculateField_management(first_stat_table, "POLY_VAL",  "(!VALUE! - str((!VALUE![3:])) / 1000", "PYTHON", "")
arcpy.CalculateField_management(first_stat_table, "LAND_RANK", "str(!VALUE!)[3:]", "PYTHON", "")

What am I missing in the python script?

Error Message:

File "<string>", line 2345, in execute
File "<string>", line 2852, in configZonalStatisticsTable
File "<string>", line 2997, in addFieldValueZonalStatisticsTable
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 3663, in CalculateField
raise e

ArcPy ERRORS:
ERROR 000539: SyntaxError: unexpected EOF while parsing (<expression>, line 1)

Failed to execute (CalculateField).

  • 1
    Please edit your question to include details about your data, what you are trying to do with the script, and the error messages you receive or anything else that makes you say "it is not working". – Midavalo Apr 21 '18 at 0:53
  • @Luke and Midavalo, please look at the updated question. I have incorporated your comments. – wondim Apr 21 '18 at 4:15
  • Yes, that is true. When I applied @Midavalo I got this error message: ERROR 000539: Error running expression: (1001 - str(1001[3:])) / 1000 Traceback (most recent call last): File "<expression>", line 1, in <module> TypeError: 'int' object has no attribute '__getitem__' Failed to execute (CalculateField). – wondim Apr 21 '18 at 4:29
2

Without more info such as error messages, we can only guess as to why your script isn't working.

First obvious problem I see is the missing parenthesis - You open 3 but close 2:

"(!VALUE! - str((!VALUE![3:])) / 1000"

I would also set your python parser to PYTHON_9.3 instead of PYTHON

Your error TypeError: 'int' object has no attribute 'getitem' will possibly be because you've put the [3:] in the wrong place. You need to put it after the convert to str, not inside:

str(!VALUE!)[3:]

And as @RJJoling has commented, you'll probably need to include the int() around it to enable the calculation of the values.

int(str(!VALUE!)[3:])

And as your VB expression is taking the right three characters you will want to use [-3:] rather than [3:] (which gives all text after the first 3)

int(str(!VALUE!)[-3:])
  • Yes, that is one of the problem but there is now another error message. ERROR 000539: Error running expression: (1001 - str(1001[3:])) / 1000 Traceback (most recent call last): File "<expression>", line 1, in <module> TypeError: 'int' object has no attribute '__getitem__' – wondim Apr 21 '18 at 4:27
  • 2
    This will result in another error, because you will try to do arithmetics with an integer and a string. This will probably do the trick: int(str(!VALUE!)[3:]) – RJJoling Apr 21 '18 at 4:50
  • Could you change it to (!VALUE!)[3:]1 to (!VALUE!)[-3:]? The rest of your solution also helped. – wondim Apr 22 '18 at 16:42
  • Please see the fourth example in this answer. stackoverflow.com/a/663175/3674573 – wondim Apr 22 '18 at 17:19

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.