I'm having trouble converting these expression to Python 3, could someone help me?
arcpy.CalculateField_management(Featureclass1, "test", "Len( [ClassRoomCd] )", "VB", "")
arcpy.CalculateField_management(Featureclass2, "ClassRoomCd", "/"0/"+ [ClassRoomCd]", "VB", "")
I tried the following expressions, but I got "the calculation tool is invalid" error.
arcpy.CalculateField_management(Featureclass1, "test", "Len( !ClassRoomCd! )", "PYTHON", "")
arcpy.CalculateField_management(Featureclass2, "ClassRoomCd", "/"0/"+ !ClassRoomCd!", "PYTHON", "")
I'm not sure what the last argument (the empty one) means.
Thank you in advance.
Dear Rema,
I hope you rae doing well,
you should pass block of the code in the empty one based on the expression which wyou provided.
please check the below link to see more in details.
https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-field.htm
I have attached a simple python script with a simple ArcGIS Project to the attachment.
This project is creating a new field and based on OBJECTID puts a value to the new created field.
Please find some screens of the project and Python script and output of the script.
Good Luck
Dear Amir Sarrafzadeh Arasi
Thank you for your reply.
I tryed the script that you atatched, and that was educational!
The script was so simple that even I can understand.
I succeeded to fill a new field with the values I defined in the script.
This experience motivated me to study python.
Thank you so much!
Dear Rema,
My Pleasure
Best Wishes
In python to get the size/length len not Len
but that only works if the field is a text field, hence
len(!ClassRoomCd! )
to add a 0 to the value, perhaps
"'0' + len(!ClassRoomCd!) "
assuming that there are no blanks/nulls in the field
@Rema wrote:I'm not sure what the last argument (the empty one) means
You can always check the reference t see what input the arcpy tools are expecting. In the case of the CalculateField function, the empty parameter is associated to the code block to be passed to the function.
https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-field.htm
Check if this format works for you:
arcpy.management.CalculateField(FeatureClass1, 'test', """f"{len(!ClassRoomCd!)}"""", "PYTHON3")
# This one may not work because target field and the field used for calculation are the same!
arcpy.management.CalculateField(FeatureClass2, 'ClassRoomCd', """f"/ {!ClassRoomCd!}"""", "PYTHON3")
Dear Raul
I'm sorry for thanking you so late.
That's what I wanted to know!
I read the link you atattched for me, and got what the "code block" is.
Thank you.