ArcPy数据处理应用之批量字段赋值

该任务要求从文本文件中读取配置信息,根据配置信息中的图层名和新旧字段值对应情况,批量将取值为旧值的要素相应字段赋以新值。

如,A图层BM字段有唯一值11、12、13,其中13需要修改为15,配置文件中保存有相应的配置项即“A,13,15”,其他唯一值11、12的数据不修改。

ArcPy处理代码如下:

... n = 0
... arcpy.env.workspace=r'D:/data.gdb'
... f = open(r'D:/T4_CodeMap.txt','r')
... for line in f.readlines():
...     n = n + 1
...     data = line.strip().split(',')
...     if len(data)==3 and arcpy.Exists(data[0]):
...         exp = u"{0} if !YSDM!=='{1}' else !YSDM!".format(data[2],data[1])
...         print u"{0} Cal: {1}, exp: {2}".format(n, data, exp)
...         arcpy.CalculateField_management(data[0],"YSDM",exp,"PYTHON_9.3")
...     else:
...         print u"{0}, Skip".format(n)
... f.close()

读取到的配置文件内容(截取部分)如下:

CSLDX_KDCSG,810304,810304102
CSLDX_KDCSG,820100,820100002
CSLDX_KDCSG,820200,820200002
DLJ_KDCSG,330101011,330101012
DLJ_KDCSG,380403003,380403002
DLJ_KDCSG,380506023,380506022
DLJ_KDCSG,810302013,810302012
DLJ_KDCSG,820200013,820200012
GDM_KDCSG,810100002,810100003
GDM_KDCSG,810300,810300003
GDM_KDCSG,810301,810301003
GDM_KDCSG,810302,810302003
GDM_KDCSG,810303,810303003
GDM_KDCSG,810304,810304003
GLM_KDCSG,40600013,406000133
GLM_KDCSG,420201034,420201033
GLM_KDCSG,420301014,420301013
GLM_KDCSG,420500014,420500003
GLM_KDCSG,420600014,420600013

?

转载自:https://blog.csdn.net/a_dev/article/details/89413376

You may also like...

跳至工具栏