ValidateFieldName (arcpy)

摘要

获取字符串(字段名)和工作空间路径,并基于输出地理数据库中的名称限制返回一个有效字段名。输入字符串中所有的无效字符都将替换为下划线 (_)。字段名的限制取决于所使用的特定数据库(结构化查询语言 [SQL] 或 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

基于当前或指定工作空间返回一个包含有效字段名的字符串。

代码实例

ValidateFieldName 示例

基于工作空间返回一个有效字段名。

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

相关主题

5/10/2014