Why does arcpy.ValidateTableName not replace hyphens (-) when used in a script outside Arc GIS Pro?
From inside Arc GIS Pro
from script but with the ArcGIS Pro python interpreter
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?
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.
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