Last active
May 4, 2016 22:26
-
-
Save hwidlund/05dcbc326f8a0cd992b0755584b84258 to your computer and use it in GitHub Desktop.
Code snippet for comparing string field lengths using dictionaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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