arcpy.da.SearchCursor got the unexpected string from the field

1800
16
Jump to solution
02-13-2021 05:34 PM
SLH
by
New Contributor II

Hi, there, 

I am learning Arcpy from ArcGIS Desktop 10.6.1. I have a feature class which has a field called "CondSize". This field has some texts such as '1/0', '2/0', '500', '1' etc. When I used the below python code, it gave me the different strings such as r'43', r'37', etc. I also tried to use str(row[0]), but no luck. I know the issue came from '/' in some strings in the field. But I don't know how to get the original strings. Your clue is appreciated.

with arcpy.da.SearchCursor(fc, ['CondSize']) as cursor:
for row in cursor:
print row[0] 

Thanks a lot

Eddy. H.

0 Kudos
16 Replies
DavidPike
MVP Frequent Contributor

Is it not just returning the coded value domain number?

View solution in original post

Amadeus111
Occasional Contributor II

@DavidPike  mentions . I think something is wrong with your domain. Check what is going on with your domain on that field or remove the domain from the field and try your code again.

View solution in original post

curtvprice
MVP Esteemed Contributor

Au contraire, I believe the coded domain is working exactly as it should!

0 Kudos
curtvprice
MVP Esteemed Contributor

Dear @SLH --

Your CondSize field has a coded domain applied to it.

This means that what is displayed in a table and in the attribute window and the identify tool is not the value in the field, but the description from the domain. your values 43, 37 are codes in the domain and values like 1/0, 2/0 are descriptions of those codes. This is very handy as it allows you to efficiently store and handle codes in fields - but expose easily understood text descriptions while editing and displaying the data.

To see the domain, go and look for it by right clicking your geodatabase where the feature class lives and select Domains and find the ConductorSize domain and look at its codes and descriptions.

ArcGIS ArcMap help: Maintaining attribute integrity while editing

ArcMap has an option to display the data instead of the code descriptions if you want.

Customize > ArcMap Options > Tables - uncheck the box "Display coded value domains and subtype descriptions"

View solution in original post

0 Kudos
DavidPike
MVP Frequent Contributor

I think a few people have converged on this being your Domain value being returned rather than the description.

The post here details it well, along with a dictionary solution by @RussellBrennan  Solved: Accessing Coded Domain Values using python - GeoNet, The Esri Community

@BrandonBarnett also links to Transfer Geodatabase Field Properties (Environment setting)—ArcGIS Pro | Documentation - an environment setting.  I'm unsure if this will work with a cursor but it is worth a go for a single additional line of code:

# Set the transferGDBAttributeProperties environment to True
arcpy.env.transferGDBAttributeProperties = True

View solution in original post

curtvprice
MVP Esteemed Contributor

Just a reminder: OP is working in ArcMap 10.6.

But, thank you, that new environment setting is useful! Why I hang out here, you always learn something.

Snap I didn't see there were answers already - still getting used to the new interface.

SLH
by
New Contributor II

Thank everyone for the reply. Yes, as @DavidPike mentioned, it was returning the coded value domain number. So I changed python code to create a new table, and then created a dictionary for number/Description to solve it. I posted the below code to benefit other learners like me. Thank everyone again!!!  

arcpy.DomainToTable_management(gdbfile, 'CondSize', "in_memory\CONDSIZE", "CODE", "DESCRIPTION")
CodeDict = {}
rows = arcpy.SearchCursor("in_memory\CONDSIZE")
for row in rows:
CodeDict[row.CODE] = row.DESCRIPTION
print (row.CODE, row.DESCRIPTION)