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

1811
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
5 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

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?


... sort of retired...

View solution in original post

0 Kudos
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

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

16 Replies
VickyWang
Esri Contributor

Try to replace your printing string to be formatted as a Unicode string :
print (u'{0}'.format(row[0]))

 

~Vicky Wang~
0 Kudos
SLH
by
New Contributor II

Hi, Vicky, thanks for the reply. I have tried it, but it is still same issue. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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?


... sort of retired...

View solution in original post

0 Kudos
SLH
by
New Contributor II

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.

SLH_1-1613273792232.png

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
VickyWang
Esri Contributor

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. 

 

~Vicky Wang~
0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
VickyWang
Esri Contributor

Thanks for the correction @DanPatterson

 

~Vicky Wang~
0 Kudos
DanPatterson
MVP Esteemed Contributor

@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


... sort of retired...
0 Kudos