Hello All,

I have a very simple search cursor that is selecting from a table a list of names in order to query a database. It was working fine until I encountered a name with an apostraphe in it. O'Brien for instance. The search cursor is very basic.

inCur = gp.SearchCursor(myFeatureClass2,"COLLECTED_BY = '" + str(Name) + "'", "", "", "DATE_COLLECTED A")

The term Name is retrieved from a list of values. When run it gets to O'Brien in the list and says that a value cannot be found. I know this is happening because of the single quotes that are around my input variable however I can't seem to find another way to word my search cursor to still find the names.

Any suggestions.

- Lee

7 Years
Discussion Span
Last Post by leeggodfrey

Try it the other way around?

inCur = gp.SearchCursor(myFeatureClass2,'COLLECTED_BY = "%s"' % (str(Name)), \
                                         "", "", "DATE_COLLECTED A")

Generally, the name is also stored as "obrien" so you can find O'Brien, O'brien, Obrien, etc. I don't know if this is possible here or not.

Edited by woooee: n/a


Thanks for the advice guys. In the end the solution was foudn as follows.

if "O'Brien" in Name:
        Name = Name.replace("'", "''")
    print 1
    inCur = gp.SearchCursor(myFeatureClass2,"COLLECTED_BY = '" + str(Name) + "'", "", "", "COLLECTED_BY A")

Adding the second ' allowed for the cursor to search normally.

Thanks again for the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.