1. 任务目标
需要将DBF文件中某一个字段下的内容进行替换
2. 完成方案
使用arcpy.da.UpdateCursor()函数完成
3. 操作实例
# encoding: utf-8
import arcpy
# 设置环境
this_year='2020'
arcpy.env.workspace = r'E:\02-Risk\World\NoPl'
# DBF文件路径
dbf_file = r'E:\Zon_Pop\{}\{}NoPlant2_Zon.dbf'.format(this_year,this_year)
# 获取并打印字段名,查看需要更改的字段名
fields = arcpy.ListFields(dbf_file)
print("字段名:")
for field in fields:
print(field.name)
# 形成替换关系字典
replacements = {
"Russia": "Russian",
"United States": "America",
"Czech Republic": "Czech",
"South Korea": "Korea",
"Slovakia": "Slovak"
}
# 使用UpdateCursor更新字段
with arcpy.da.UpdateCursor(dbf_file, ["NAME_0"]) as cursor:
for row in cursor:
if row[0] in replacements:
row[0] = replacements[row[0]] # 根据替换关系字典更新值
cursor.updateRow(row)
print("更新完成")