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.
Solved! Go to Solution.
Can you provide specifics on the field type.
Is definitely a string/text field or something else?
Are there missing values?
And regionalization/language settings?
Have you tried the TableToExcel tool to examine the table in Excel?
Is it not just returning the coded value domain number?
@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.
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"
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
Hi, Vicky, thanks for the reply. I have tried it, but it is still same issue.
Can you provide specifics on the field type.
Is definitely a string/text field or something else?
Are there missing values?
And regionalization/language settings?
Have you tried the TableToExcel tool to examine the table in Excel?
Hi, Dan,
I double checked the field and copied it as below. It is text. And also I tried to export it as txt file and excel file. It is same issue there: it gave me the different strings such as r'43', r'37', etc
Thanks
Eddy H.
weird... or the field is weird. Try adding a new text field to the table and do a simple field calculate, python parser eg your new field would equal
!Condsize!
if that doesn't work try a couple of other variants like
val = str(row[0]).replace("\\", "_")
print(val)
I just want to check to see if it can be converted to pure text and replacing the backslashes with underscores or not.
Best check is get on an ArcGIS Pro machine and try it with python 3.7. python 2.7 is done as is the arcmap series (although it will remain on support for the foreseeable future.
It doesn't appear that the unicode or raw encoding worked for you (?!) but again, you are using python 2.7
feel wired about the field too.
I used ArcMap 10.8.1 with Unicode string formation that works. try to replace the string as what @DanPatterson suggests. replace by two backslashes.
Create a new field and use field calculation to convert the existing field value to string in the new field.
his problem isn't backslashes, but foreslashes '1/0',
which makes it more confusing, hence the suggestion of replacing with anything other than a slash
@VickyWang , sorry for the confusion, I meant any "slash" since they have meaning in unicode (see "solidus" in unicode). By replacing and \,/ with _ removes those from causing a misinterpretation of anything before or after it.
In any event ArcGIS Pro and python 3 would be a better test