Select to view content in your preferred language

Convert expression in Calculate Field from VB to PYTHON3

474
6
01-09-2024 01:20 AM
Rema
by
New Contributor II

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.

0 Kudos
6 Replies
Amir-Sarrafzadeh-Arasi
Occasional Contributor

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.

MAP_1.png

ATT_1.png

Py_1.png

ATT_2.png

    

Good Luck

Amir Sarrafzadeh Arasi
Rema
by
New Contributor II

Dear screens_38.pngAmir 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!

Amir-Sarrafzadeh-Arasi
Occasional Contributor

Dear Rema, 
My Pleasure

 

Best Wishes

Amir Sarrafzadeh Arasi
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
Raul
by
New Contributor III

@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")

 

 

 

Rema
by
New Contributor II

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.

0 Kudos