Select to view content in your preferred language

Apostrophe in query

1332
4
Jump to solution
11-19-2012 06:46 AM
ionarawilson1
Deactivated User
How can I make this query work?

    query = '"CLASS" = \'Z'berg Park\''

I know it is not working because of the apostrophe on the name. Any ideas on what I should change? Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Does this work for you?
query = """"CLASS" = 'Z''berg Park'"""

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Honored Contributor
Does this work for you?
query = """"CLASS" = 'Z''berg Park'"""
0 Kudos
ionarawilson1
Deactivated User
Matthew,

Thanks for your help. But I am still getting an error message when I run the code. Please see code snippet:

import arcpy, sys, traceback

arcpy.env.workspace = "C:\\PYTHON_PRIMER\\Chapter05\\Data\\"

street_class2 = "Sacramento_Streets.shp"
street_layer2 = "Streets"
out_streets2 = "out_streets.shp"
neighborhood_class2 = "Sacramento_Neighborhoods.shp"
neighborhood_layer2 = "Neighborhoods"




try:

    # 1. Make a feature layer for streets

    if arcpy.Exists(neighborhood_layer2):
        arcpy.Delete_management(neighborhood_layer2)

    arcpy.MakeFeatureLayer_management(neighborhood_class2, neighborhood_layer2)

    # 2. Create a query statement to select highways

    query = """"CLASS" = 'Z''berg Park'"""

    # 3. Select by attributes using the query

    # Select features by attribute using query
    arcpy.SelectLayerByAttribute_management(neighborhood_layer2, "NEW_SELECTION", query)
    result2 = arcpy.GetCount_management(neighborhood_layer2)
    print "Number of selected features in the feature layer " + neighborhood_layer2 + " with a query: "+ str(result2)

    # 4. Make feature layer

    if arcpy.Exists(street_layer2):
        arcpy.Delete_management(street_layer2)

    arcpy.MakeFeatureLayer_management(street_class2, street_layer2)


   # Create query statement
    query2 = '"CLASS" = \'C\''


   #4.a Select "local" streets inside Meadowview
    arcpy.SelectLayerByAttribute_management(street_layer2, "NEW_SELECTION", query2)


And the error message:

PYTHON ERRORS:
Traceback Info:
  File "C:\PYTHON_PRIMER\Chapter05\exercise5extra.py", line 48, in <module>
    arcpy.SelectLayerByAttribute_management(neighborhood_layer2, "NEW_SELECTION", query)

Error Info:
     <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).




Line 48 is:  arcpy.SelectLayerByAttribute_management(neighborhood_layer2, "NEW_SELECTION", query)
0 Kudos
ChrisSnyder
Honored Contributor
This format works for me:

query = "CLASS = 'Z''berg Park'"
0 Kudos
ionarawilson1
Deactivated User
Chris, your expression did not work on my case but Matthew's expression did after I changed the name of the field (it was not CLASS but NAME):

    query = """"NAME" = 'Z''berg Park'"""

Thank you both for your help!!!
0 Kudos