My goal is to rename shapefiles in 2 folders via transliteration using arcpy. That means that I want to replace Cyrillic (Russian) symbols with Latin ones. Here is my code, that doesn't work. What's wrong with it?

import arcpy

InWork =  ["C:\\Users\Vadim\Downloads\GML\esri", "C:\\Users\Vadim\Downloads\GML\stp"]

for ws in InWork:
    arcpy.env.workspace = ws
    datasets = arcpy.ListFeatureClasses()
    for fc in datasets:
        newName = fc.replace(u"абвгдеёзийклмнопрстуфхъыьэАБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЬЭ",u"abvgdeezijklmnoprstufh'y'eABVGDEEZIJKLMNOPRSTUFH'Y'E")
        arcpy.Rename_management(fc, newName[:-4])
  • 2
    This appears to be more of a Python issue (relating to how string.replace() works) than a GIS-specific task. – Vince Oct 4 at 16:08

If you use string.replace here, you will have to run it separately for each character in your string which will be tedious and inefficient.

I think a better approach is to use a dictionary (which will allow for fast lookup) and a string builder (for fast construction).

See the answer from @Hans on this post: https://stackoverflow.com/questions/2835637/net-string-replace-russian-to-english

Also this package is designed to do character to character translation so you might try it out first and see if you get the desired results: https://pypi.org/project/Unidecode/

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