1

Using Arcpy in Python, I would like to return / print '0.15' from the following table: table

When I use the following code I get "(u'D', 0.20)" as my print statement in Python:

import arcpy
fc = "C:/Users/.../table_test2"
q = 'C'
with arcpy.da.SearchCursor(fc, ['Index_Score'], where_clause=q) as cursor:
    print row


(u'D', 0.20)

This is not what I am looking for as I am looking for the value '0.15'.

When I tried changing my query statement 'q' to: q = '"SA_ALL" = "C"' I get the error:

" Traceback (most recent call last):
File "<pyshell#143>", line 2, in <module>
for row in cursor:
RuntimeError: An expected Field was not found or could not be retrieved properly.    [table_test2]"

Can someone help me and explain what I am doing wrong? Also what does the 'u' mean in my first returned print statement?

2

You might look into using the AddFieldDelimiters function. It can make writing queries easier.

import arcpy

fc = "C:/Users/.../table_test2"
query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(fc, "SA_ALL"), "C")

with arcpy.da.SearchCursor(fc, ("SA_ALL", "Index_Score"), query) as cursor:
    for row in cursor:
        print row[1]

Using just "C" is not a proper query.

  • I have tried using this method but I get no print statement nor any errors. query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(fc, "SA_ALL"), "C") with arcpy.da.SearchCursor(fc, ("SA_ALL", "Index_Score"), query) as cursor: for row in cursor: print row[0] print tow[1] >>> – Jelle Feb 18 '15 at 16:57
  • @Jelle hmm I'm not sure why it wouldn't work because it's essentially the exact same query Ben Gosack suggested. – ianbroad Feb 18 '15 at 17:10
4

There are a couple of issues that are occurring here. First of all formatting of a query string can be a bit tedious, the delimiter you use for the field name is dependent on the input data type (shapefile, personal geodatabase, file geodatabase). See additional documentation. For a file geodatabase the delimiter is a double quote, but the string delimiter is a single quote, to do this you will need to use escape characters or string formatting.

Try this:

'"SA_ALL" = \'C\''

The second issue is accessing the data element you want out of the cursor object. Add a for loop to iterate the rows returned in your cursor, even though you may only return one row. The returned object will be a list, so index the position of the item in that list. If you are still running into a format issue, use str(row[0]) or float(row[0]) to cast the output into the format you want.

with arcpy.da.SearchCursor(fc, ['Index_Score'], '"SA_ALL" = \'C\'') as cursor:
    for row in cursor:
        print row[0]
  • Thanks for your answer. This certainly did the trick. I am indeed using geodatabase files so I used excape characters. How would you use escape characters if I want to replace 'C' with a defined variable? – Jelle Feb 18 '15 at 16:56
  • @Jelle, I think you would then want to use the string format function: '"SA_ALL" = \'{0}\''.format(variable) – Ben Gosack Feb 18 '15 at 17:19
1

This example uses a dictionary comprehension to convert your two fields into a dictionary and then simply prints the value based on your query check["C"]:

import arcpy

fc = r'C:\temp\test.gdb\test'

check = {row[0]: row[1] for row in arcpy.da.SearchCursor(fc, ("SA_ALL", "Index_Score"))}
print check["C"]

To answer the second part of your question, u'' indicates a unicode string data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.