arcpy表连接与字段名更改

注意不能用AlterField_management修改shp文件的字段名称,会报错

# encoding:utf-8
import arcpy
import os

if __name__ == '__main__':
    arcpy.env.workspace = r'G:\字段连接'
    dbfs = os.listdir('./Dpm')
    dbfs = filter(lambda dbffile: dbffile.endswith('.dbf'), dbfs)
    joinShp = r'G:\字段连接\Dpm\温县.shp'
    count = 0
    fieldType = None
    for dbf in dbfs:
        joindbf = r'G:\字段连接\Dpm\{}'.format(dbf)
        arcpy.JoinField_management(in_data=joinShp, in_field='FID', join_table=joindbf, join_field='FID_',
                                   fields=['MEAN'])
        dbfname = dbf[3:-4]
        meanfield = arcpy.ListFields(joinShp, 'MEAN')[0]
        if count == 0:
            fieldType = meanfield.type
        arcpy.AddField_management(joinShp, 'MEAN{}'.format(dbfname), fieldType)
        arcpy.CalculateField_management(joinShp, 'MEAN{}'.format(dbfname), "!" + 'MEAN' + "!", "PYTHON", "")
        arcpy.DeleteField_management(joinShp, 'MEAN')
        count += 1
        print ('{}:{}'.format(count, dbf))
    print ('finished!')

你可能感兴趣的:(Arcpy与ArcGIS,arcpy,python,gis,arcgis)