Creating a personal tool - Scripting - Failure to execute expression

252
1
Jump to solution
05-17-2022 06:33 AM
KalinaStoycheva
New Contributor II

Hello,

I just started using python so I suspect the answer to my question is as obvious to you as it isn't to me. I have been trying to create a personal tool that allows a person to transform a field with Cyrillic text in a feature class into a new field by replacing every character with the corresponding Latin one. 

for the tool I have set the following parameters

KalinaStoycheva_0-1652793615196.png

where Output feature class is an output and everything else is an input

the script is in PyCharm and looks like this:

 

import arcpy

# coding=utf-8
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
Input_Feature_Class = arcpy.GetParameterAsText(0)
New_Latin_field = arcpy.GetParameterAsText(1)
Cyrillic_Field = arcpy.GetParameterAsText(2)
Output_Feature_Class3 = arcpy.GetParameterAsText(3)


Output_Feature_Class = Input_Feature_Class
Output_Feature_Class_2 = Output_Feature_Class
Field_Name = New_Latin_field
Field_Name__2_ = New_Latin_field
expression2 = '!'+New_Latin_field+'!'+".replace(u\"\\u0410\", " \
"\"A\").replace(u\"\\u0430\", " \
"\"a\").replace(u\"\\u0411\", " \
"\"B\").replace(u\"\\u0431\", " \
"\"b\").replace(u\"\\u0412\", " \
"\"V\").replace(u\"\\u0432\", " \
"\"v\").replace(u\"\\u0413\", " \
"\"G\").replace(u\"\\u0433\", " \
"\"g\").replace(u\"\\u0414\", " \
"\"D\").replace(u\"\\u0434\", " \
"\"d\").replace(u\"\\u0415\", " \
"\"E\").replace(u\"\\u0435\", " \
"\"e\").replace(u\"\\u0416\", " \
"\"J\").replace(u\"\\u0436\", " \
"\"j\").replace(u\"\\u0417\", " \
"\"Z\").replace(u\"\\u0437\", " \
"\"z\").replace(u\"\\u0418\", " \
"\"I\").replace(u\"\\u0438\", " \
"\"i\").replace(u\"\\u0419\", " \
"\"I\").replace(u\"\\u0439\", " \
"\"i\").replace(u\"\\u041A\", " \
"\"K\").replace(u\"\\u043A\", " \
"\"k\").replace(u\"\\u041B\", " \
"\"L\").replace(u\"\\u043B\", " \
"\"l\").replace(u\"\\u041C\", " \
"\"M\").replace(u\"\\u043C\", " \
"\"m\").replace(u\"\\u041D\", " \
"\"N\").replace(u\"\\u043D\", " \
"\"n\").replace(u\"\\u041E\", " \
"\"O\").replace(u\"\\u043E\", " \
"\"o\").replace(u\"\\u041F\", " \
"\"P\").replace(u\"\\u043F\", " \
"\"p\").replace(u\"\\u0420\", " \
"\"R\").replace(u\"\\u0440\", " \
"\"r\").replace(u\"\\u0421\", " \
"\"S\").replace(u\"\\u0441\", " \
"\"s\").replace(u\"\\u0422\", " \
"\"T\").replace(u\"\\u0442\", " \
"\"t\").replace(u\"\\u0423\", " \
"\"U\").replace(u\"\\u0443\", " \
"\"u\").replace(u\"\\u0424\", " \
"\"F\").replace(u\"\\u0444\", " \
"\"f\").replace(u\"\\u0425\", " \
"\"H\").replace(u\"\\u0445\", " \
"\"h\").replace(u\"\\u0426\", " \
"\"Ts\").replace(u\"\\u0446\", " \
"\"ts\").replace(u\"\\u0427\", " \
"\"Ch\").replace(u\"\\u0447\", " \
"\"ch\").replace(u\"\\u0428\", " \
"\"Sh\").replace(u\"\\u00448\", " \
"\"sh\").replace(u\"\\u0429\", " \
"\"Sht\").replace(u\"\\u0449\", " \
"\"sht\").replace(u\"\\u042A\", " \
"\"U\").replace(u\"\\u044A\", " \
"\"u\").replace(u\"\\u042B\", " \
"\"Y\").replace(u\"\\u044B\", " \
"\"y\").replace(u\"\\u042C\", " \
"\"Y\").replace(u\"\\u044C\", " \
"\"y\").replace(u\"\\u042D\", " \
"\"E\").replace(u\"\\u044D\", " \
"\"e\").replace(u\"\\u042E\", " \
"\"Yu\").replace(u\"\\u044E\", " \
"\"yu\").replace(u\"\\u042F\", " \
"\"Ya\").replace(u\"\\u044F\", " \
"\"ya\")"

arcpy.AddField_management(in_table=Input_Feature_Class,
field_name=New_Latin_field,
field_type="TEXT",
field_precision=None,
field_scale=None,
field_length=254,
field_alias=None,
field_is_nullable="NULLABLE",
field_is_required="REQUIRED")


arcpy.CalculateField_management(in_table=Output_Feature_Class,
field=New_Latin_field,
expression=Cyrillic_Field,
expression_type="PYTHON_9.3")



arcpy.CalculateField_management(in_table=Output_Feature_Class_2,
field=Field_Name__2_,
expression=expression2,
expression_type="PYTHON_9.3")

 when I run it  the expression is supposed to take input Cyrillic field and copy it to a new field, but I receive the following error:

Executing: CyrillicToLatin "D:\ГИС Пространствени данни\GIS_BG_back-up.gdb\Transport\BG_Airport" Latin1 name
Start Time: Tue May 17 16:24:38 2022
Running script CyrillicToLatin...
Failed script CyrillicToLatin...

Traceback (most recent call last):
File "D:\ГИС Пространствени данни\Toolboxes\Python scripts\cyrtolat.py", line 98, in <module>
expression_type="PYTHON_9.3")
File "c:\program files (x86)\arcgis\desktop10.8\arcpy\arcpy\management.py", line 3646, in CalculateField
raise e
ExecuteError: ERROR 000539: Error running expression: name
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'name' is not defined

Failed to execute (CalculateField).


Failed to execute (CyrillicToLatin).
Failed at Tue May 17 16:24:39 2022 (Elapsed Time: 0,55 seconds)

 

where "Latin1" is the new name I have given for the Latin field, and "name" is the existing Cyrillic field selected from running the tool.

KalinaStoycheva_1-1652794347621.png

 

I suspect that the problem comes from defining the two input fields as fields and using them in the expression, however I cannot figure out how to correct it.

Any suggestions would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

Fields in a calculate field Python expression need to be enclosed in exclamation marks. The error is referring to "name", which is the value you entered for the Cyrillic field tool parameter. You did it correctly for expression2, but the expression argument for the first calculate field should be '!'+Cyrillic_Field+'!'

View solution in original post

1 Reply
BlakeTerhune
MVP Regular Contributor

Fields in a calculate field Python expression need to be enclosed in exclamation marks. The error is referring to "name", which is the value you entered for the Cyrillic field tool parameter. You did it correctly for expression2, but the expression argument for the first calculate field should be '!'+Cyrillic_Field+'!'