操作方法

操作方法:在 ArcGIS Pro 中使用 Python 更改字段别名

Last Published: April 7, 2023

摘要

在 ArcGIS Pro 中,字段工具集包含用于创建和修改任何地理数据库表或要素类的字段的工具。 可以通过在 ArcGIS Pro 中运行 Python 脚本来进行这些修改,本文提供了工作流。

下图显示了 Schools_all 属性表,其中字段别名包含下划线和小写字母。

Schools_all 属性表

过程

  1. 启动 ArcGIS Pro 并打开该工程。
  2. 要打开 Python 窗口,请在顶部功能区上单击分析,单击 Python 下拉列表箭头并选择 Python 窗口
“分析”选项卡上“Python”下拉框中的 Python 窗口图标
  1. Python 窗口中指定以下脚本。
    1. 导入系统模块。
import arcgis
import os
  1. 指定 ArcPy 函数以检查扩展名并覆盖输出。
arcpy.env.overwriteOutput = True
  1. 指定变量 Syntax 以列出图层的所有字段名称。
<variableName> = arcpy.ListFields('<layerName>')
  1. 指定 ArcPy 函数以将更改应用于字段别名。 在此示例中,首字母大写,“_”替换为空格。
print("Changing the field alias to field name by Capitalizing the initial letter")
for field in <variableName>:
    if not field.required:
        arcpy.management.AlterField('<layerName>', field.name, field.name, field.name.rstrip(field.name[-2:]))
        arcpy.management.AlterField('<layerName>', field.name, field.name, field.name.replace("_"," ").title())
        print("Completed")

以下代码块演示了完整的工作脚本。

import arcpy
import os
arcpy.env.overwriteOutput=True
infeature=arcpy.GetParameterAsText(0)
fields=arcpy.ListFields('Schools_all')
print("Changing the field alias to field name by Capitalizing the initial letter")
for field in fields:
    if not field.required:
        arcpy.management.AlterField('Schools_all', field.name, field.name,field.name.rstrip(field.name[-2:]))
        arcpy.management.AlterField('Schools_all', field.name, field.name, field.name.replace("_"," ").title())
        print("Completed")

下图显示了在 ArcGIS Pro 中使用 Python 更新的字段别名。

部分 Python 脚本和更新的字段别名的结果

文章 ID: 000029252

接收通知并查找新问题或常见问题的解决方案

从我们全新的 AI 聊天机器人中获得简明答案和视频解决方案。

下载 Esri 支持应用程序

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项