1

I am writing a script which supposed to get a value for a field from user and use that value for a definition query.

If I write it without variable, it works:

my_Layer.definitionQuery = "IncidentName = 'Hospital Rd, Kurri Kurri'"

If I this, it does not work:

AA = 'Hospital Rd, Kurri Kurri'
my_Layer.definitionQuery = "IncidentName = AA"

The definition query is set to string AA, and not the value set I assigned to it.

2

You have just made AA a literal string by quoting it. You have not set up the quotes correctly to make the single quotes literal within your AA variable, plus you have not concatenated the variable value to the string or added it to the string using the .format syntax. There are several ways to do it correctly. For example it could be done this way:

AA = "'Hospital Rd, Kurri Kurri'"
my_Layer.definitionQuery = "IncidentName = {}".format(AA)

Alternatively it could be done this way, which would make it easier for the user to input a value that did not have to be properly quoted:

AA = "Hospital Rd, Kurri Kurri"
my_Layer.definitionQuery = "IncidentName = '{}'".format(AA)
3
  • That worked. Thank you! Do you know where I can find more information on all different quoting types for arcpy? – lida Dec 7 '15 at 10:01
  • arcpy does not have a discussion on "quoting types". Python treats both double quotes and single quotes as strings, and by enclosing one type of quote within the other you can make the enclosed quotes a literal part of the string. SQL requires single quotes for strings that are part of the SQL expression. Python.org gives some help, but it is not great docs.python.org/2.0/ref/strings.html Search Goolge for better help. – Richard Fairhurst Dec 7 '15 at 15:51
  • 1
    @lida if it works please mark as solved – FelixIP Dec 7 '15 at 22:44

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.