ValidateFieldName

Resumen

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 (Structured Query Language [SQL] or Oracle).

Sintaxis

ValidateFieldName (name, {workspace})
ParámetroExplicaciónTipo de datos
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 personal, 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
Valor de retorno
Tipo de datosExplicación
String

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

Muestra de código

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.AddField_management(in_fc, field_name, "DOUBLE")
    arcpy.CalculateField_management(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))

Temas relacionados