I have a SQL expression that I use in Make Feature Layer in Model Builder ("HOSPITAL" = '% HOSPITALNAME%'). The expression makes a selection in a OD Cost Matrix that a patient is patient only at one hospital and not all hospitals as an OD Cost Matrix would normally do. HOSPITAL is a column in orgins in add location and HOSPITALNAME is a column in the DESTINATION to add location. What I wonder is how this expression will be in a python script? I've tried something like this in python in the expression field in arcpy.MakeFeatureLayer "'HOSPITAL' = '" + HOSPITAL NAME + "'". But it does not works. I don't get any error message but it only runs OD Cost Matrix normally.

  • Thanks for the reply. I've got an expression that seems to work now. But I struggle with that the output files will overwrite each other. In the Model Builder you avoid that by typing for example " % Value% " in the output file. But how do you write it in Python? Tom Anders – user15888 Jan 30 '14 at 14:03
  • Could you post your Python code so we can see where the actual problem is? – Cindy Jayakumar Jan 31 '14 at 6:12

You can always play with "" and '' to open and close string parts, or put special characters after \, but it quickly get complicated with multiple symbols. You need to make sure that the string that will be read in Python will be the string that you type as a query. In your case, this would be :

'"fieldName" = " ' + variable_with_the_fieldname + '"'  


"fieldname" = "otherfieldname"


'"fieldName" = ' + "'" + variable_with_string_value + "'"  


"fieldname" = 'stringvalue'

The more easy way is to use the triple """ so that you don't have to worry about 's and "s anymore inside your string.

Remark, with some database (e.g. personnal geodatabase), the name of the field must be in [] instead of "".

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.