ValidateFieldName

Synthèse

Takes a string (field name) and a workspace path and returns a valid field name based on name restrictions in the output geodatabase. All invalid characters in the input string will be replaced with an underscore (_). The field name restrictions depend on the specific database used (SQL or Oracle).

Syntaxe

ValidateFieldName (name, {workspace})
ParamètreExplicationType de données
name

The field name to be validated. If the optional workspace is not specified, the field name is validated against the current workspace.

String
workspace

An optional specified workspace to validate the field name against. The workspace can be a file system or a file, or enterprise geodatabase.

If the workspace is not specified, the field name is validated using the current workspace environment. If the workspace environment has not been set, the field name is validated based on a folder workspace.

String
Valeur renvoyée
Type de donnéesExplication
String

Returns a string containing the valid field name, based on either the current or specified workspace.

Exemple de code

ValidateFieldName example

Returns a valid field name based on the workspace.

import arcpy
import os

class ShapeError(Exception):
    pass


try:
    # Get the input feature class and make sure it contains polygons.
    in_fc = arcpy.GetParameterAsText(0)
    if arcpy.Describe(input).shapeType != "Polygon":
        # Raise a custom exception
        raise ShapeError("Input does not contain polygons")

    # Get the new field name and validate it.
    field_name = arcpy.GetParameterAsText(1)
    field_name = arcpy.ValidateFieldName(field_name, os.path.dirname(in_fc))

    # Add the new field and calculate the value.
    arcpy.management.AddField(in_fc, field_name, "DOUBLE")
    arcpy.management.CalculateField(in_fc,
                                    field_name,
                                    "!shape.area! / !shape.length!",
                                    "PYTHON_9.3")
except ShapeError as err:
    print(err[0])
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

Rubriques connexes