使用python脚本批量修改Arcgis的数据字段类型或者名称

参考其他的文章,放上代码,直接使用arcgis自带的python IDE运行,可解决中文字符的问题。

# -- coding: utf-8 --
import arcpy
import sys
from arcpy import env
# use your own
env.workspace = r"C:\数据处理\zhitu.gdb\Local" #工作空间,作者此处使用的是个人地理数据库
infc=r'zrtzz' #输入图层名称
outfc=r'zrtzz_1' #输出图层名称
outpath = r"C:\数据处理\制图文件\zhitu.gdb\Local"
#判断是否含有中文字符,因为作者需转换数据类型的字段名均含中文字符,因此加入此函数
def check_contain_chinese(check_str):
    #for ch in check_str.decode('utf-8'):  
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
        return False
#fcList = arcpy.ListFeatureClasses()
    #outfc = infc
print "in featureclass : " + infc + " out featureclass : " + outfc
fieldmappings = arcpy.FieldMappings()
infields = arcpy.ListFields(infc)
for infield in infields:
    #初步设置过滤字段
    if infield.name == "OBJECTID" or infield.name == "shape" or infield.name == "Shape" or infield.name == "Shape_Length" or infield.name == "Shape_Area":
        continue
    fieldmap = arcpy.FieldMap()
    fieldmap.addInputField(infc, infield.name)
    outname = infield.name
    outfield = fieldmap.outputField
    outfield.name = outname.upper()
    #根据需求设置转换数据类型,此处将字段中含有中文并且字段类型为双精度型的数据转为短整型
    if check_contain_chinese(infield.name) and infield.type=="Double":
        outfield.type="SmallInteger"
    fieldmap.outputField = outfield
    fieldmappings.addFieldMap(fieldmap)
    #print u"add field map " + infield.name + u" - " + outfield.name
    del fieldmap, outfield
print u"start to copy ......"
arcpy.FeatureClassToFeatureClass_conversion(infc, outpath, outfc, "", fieldmappings)
print u"done"

 

 

你可能感兴趣的:(GIS,python学习,Arcgis,python,字段类型,批量,转换)