Select to view content in your preferred language

String formatting ? for definition query

1346
4
Jump to solution
04-10-2012 09:50 AM
Zeke
by
Honored Contributor
I'm trying to set up a definition query for a layer, but can't get the formatting correct. I've tried several variations of the code below w/o success, such as writing the query out in triple double quotes (which for some reason required an extra double quote) . Appreciate any help. This kind of thing was easier in the old days of VB & VBA.
Code:
    case_number = arcpy.GetParameterAsText(0)     fld = "Case_"                                                            # tried substituting this for "Case_" in the line below, for example.     query = '"%s"' + " = " + "'%s'" % ("Case_", case_number)  #1st %s is single, double, double single quotes; 2nd is double single single double      # query should look like "Case_" = 'A-BC-12-34' where case_number = A-BC-12-34,


Error:
<type 'exceptions.TypeError'>: not all arguments converted during string formatting
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceNielsen
Frequent Contributor
I think it should look something like this:
query = "\"Case_\" = '%s'" % case_number -or- query = "\"%s\" = '%s'" % (fld, case_number)

View solution in original post

0 Kudos
4 Replies
BruceNielsen
Frequent Contributor
I think it should look something like this:
query = "\"Case_\" = '%s'" % case_number -or- query = "\"%s\" = '%s'" % (fld, case_number)
0 Kudos
Zeke
by
Honored Contributor
Thanks Bruce, that works great. I thought I had tried the escape \, but guess not correctly. How would this work for a variable, fld in the code snippet? Even though I don't expect the field name to change, I don't like using hard coded values if I can avoid it.
0 Kudos
BruceNielsen
Frequent Contributor
I edited my previous post to show both methods. The way you were doing it originally should have looked like:
query = '"%s"' % fld + " = " + "'%s'" % case_number
0 Kudos
Zeke
by
Honored Contributor
Thanks, appreciate it.
0 Kudos