ValidateFieldName (arcpy)

サマリ

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).

構文

ValidateFieldName (name, {workspace})
パラメータ説明データ タイプ
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 ArcSDE 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
戻り値
データ タイプ説明
String

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

コードのサンプル

ValidateFieldName example

Returns a valid field name based on the workspace.

class FieldError(Exception):
    pass

class ShapeError(Exception):
    pass

import arcpy
import os

try:
    # Get the input feature class and make sure it contains polygons.
    #
    input = arcpy.GetParameterAsText(0)
    dscFC = arcpy.Describe(input)
    if dscFC.shapeType.lower() != "polygon":
        # Raise a custom exception
        raise ShapeError
		
    # Get the new field name and validate it.
    #
    fieldName = arcpy.GetParameterAsText(1)	
    fieldName = arcpy.ValidateFieldName(fieldName, os.path.dirname(input))

    # Make sure shape_length and shape_area fields exist
    #
    if len(arcpy.ListFields(input,"Shape_area")) > 0 and \
        len(arcpy.ListFields(input,"Shape_length")) > 0:

        # Add the new field and calculate the value.
        #
        arcpy.AddField_management(input, fieldName, "double")
        arcpy.CalculateField_management(input,fieldName, 
                                        "[Shape_Area] / [Shape_Length]")
    else:
        # Raise a custom exception
        raise FieldError
except ShapeError:
    print "Input does not contain polygons"
except FieldError:
    print "Input does not contain shape_area and shape_length fields"
except:
    print arcpy.GetMessages(2)

関連トピック

9/14/2013