Select to view content in your preferred language

ValidateTableName inconsistent

106
2
a week ago
MortenBackNielsen
Emerging Contributor

Why does arcpy.ValidateTableName not replace hyphens (-) when used in a script outside Arc GIS Pro?

From inside Arc GIS Pro

MortenBackNielsen_1-1736257416748.png

 

from script but with the ArcGIS Pro python interpreter

MortenBackNielsen_0-1736257382235.png

Note in Arcgis Pro the hyphen is replaced by an underscore.

 

It's quick do this with standard python code, but why are there this difference and can arcpy be changed to behave the same?

 

0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor

I have reported a similar bug in 2024 which has yet to be resolved. Clearly the function is buggy and needs ESRI to resolve it.

HaydenWelch
MVP

According to the documentation , The function expects a workspace argument. When in the context of a project, that parameter is implied as "CURRENT" or the current working directory. If you are outside an active workspace or running your interpreter from outside a workspace that the table name can be validated against.

This function is implemented in the C binaries, so I can't review how it is written. It seems to just return the input value is the workspace parameter is invalid. This behavior is a bit buggy (It should raise an Exception!), but to get around it, you just need to explicitly pass a workspace to the function call.

If all you want is to do some string substitution, you could wrap the ValidateTableName function like this:

from arcpy import ValidateTableName

def validate_table_name(table_name: str) -> str:
    validated_name = ValidateTableName(table_name)
    if validated_name == table_name:
        for character in (' ', '-', '.'): # add more as needed
            validated_name = table_name.replace(character, "_")
    return validated_name
0 Kudos