Skip to content

Instantly share code, notes, and snippets.

@hwidlund
Last active May 4, 2016 22:26
Show Gist options
  • Select an option

  • Save hwidlund/05dcbc326f8a0cd992b0755584b84258 to your computer and use it in GitHub Desktop.

Select an option

Save hwidlund/05dcbc326f8a0cd992b0755584b84258 to your computer and use it in GitHub Desktop.
Code snippet for comparing string field lengths using dictionaries
import arcpy
srcFC = "c:/path/Geodatabase.gdb/srcFc"
destFC = "c:/path/Geodatabase.gdb/destFc"
# get field lists from each feature class, restricting to "String" feature types
srcFields = arcpy.ListFields(srcFC,"","String")
destFields = arcpy.ListFields(destFC,"","String")
# create empty dictionaries
srcDict = {}
destDict = {}
# populate dictionaries with key value pairs of field name: field length
# so that the properties can be accessed by field name
for srcField in srcFields:
srcDict[srcField.name] = [srcField.length]
for destField in destFields:
destDict[destField.name] = [destField.length]
# iterate through destination dictionary looking
# for matching fields in source
# access field lengths in dictionary by field name
# compare field lengths
for fieldName, fieldLen in destDict.items():
if fieldName in srcDict:
srcLen = srcDict[fieldName][0]
destLen = destDict[fieldName][0]
if destLen < srcLen:
print("ERROR String field length mismatch")
print(fieldName + " field is too long")
else:
print(fieldName + " all is good")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment