954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dealing with an Apostraphe

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

leeggodfrey
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

what module is this gp?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

module is from arcgisscripting

leeggodfrey
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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.

leeggodfrey
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: